[WP 8.1] [C#] Resume上有多个Page实例的问题 [英] [WP 8.1][C#] Problem with multiple instances of Page on Resume

查看:131
本文介绍了[WP 8.1] [C#] Resume上有多个Page实例的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用windows.media.capture API为windows phone 8.1(运行时)和visual studio 2013编写一个相机应用程序。

I'm writing an camera app using the windows.media.capture API for windows phone 8.1 (runtime) with visual studio 2013.

我有一个页面使用相机。我现在尝试添加代码来处理挂起和恢复。问题是,当从相机页面导航回主页面时,相机页面的实例没有(或没有立即)销毁/
垃圾收集。

I have one page that uses the camera. I now tried to add the code to handle suspending and resuming. The problem is, that when navigating back from the camera page to the main page, the instance of the camera page is not (or not immediately) destroyed / garbage collected.

因此,只要我再次向前导航到相机页面,就会创建一个新实例,现在有2个同一页面的实例。检查析构函数以及分析提供了有多个页面实例的信息。当
应用程序在这种情况下被重新启动然后恢复时,两个页面都会运行其OnResume方法。当我在简历中初始化相机时,两个页面都尝试初始化相机,这当然是不可能的。

So as soon as i navigate forward to the camera page again, a new instance is created and there are now 2 instances of the same page. Checking the destructor and also profiling provides the information that there are multiple instances of the page. When the App is supended in this situation and then resumed, both pages run their OnResume Method. As I am initializing the camera in the resume, both pages try to initialize the camera, which of course is not possible.

我尝试取消订阅NavigationHelper的OnNavigateFrom中的resume事件,但是暂停也调用此函数,然后恢复根本不起作用。我也没有看到检查当前类是否是"可见"的方法。一,因为
所有我能得到的是当前显示页面的类型。

I tried unscubscribing the resume event in OnNavigateFrom of the NavigationHelper, but since suspending also calls this function, resuming does not work at all then. I also see no method of checking if the current class is the "visible" one, since all i can get is the type of the current displayed page.

我认为这里的主要问题是从导航回来时页面不会被破坏它是由一些有问题的引用引起的,它使类保持活着。但我对visual studio
的开箱即用默认模板进行了测试,并在那里看到了这种行为。我创建了一个类型为"Grid App"的新项目。我唯一改变的是在ItemDetailPage上添加了一个析构函数来添加一个断点,所以我可以看到一个实例何时被创建,什么时候被销毁。

I thought that the main problem here is that the page does not get destroyed when navigating back from it and that this is caused by some problematic reference that keeps the class alive. But I did a test on an out-of-the box default template of visual studio and also see that behaviour there. I created a new Project of type "Grid App" and the only thing I changed was an added destructor on ItemDetailPage to add a breakpoint, so i can see when an instance gets created, and when destroyed.

~PoldDetailPage()

{

    int x = 1;

}

~ItemDetailPage()
{
    int x = 1;
}

当我启动应用程序并单击某个项目以进入ItemDetailPage,然后返回时,实例将被销毁立即。然后我再次向前导航到ItemDetailPage,然后再返回,并且实例保持未被破坏状态。我甚至可以点击其他项目
来获取ItemDetailPages并返回。所有实例都保持活着。经过一段随机时间后,所有人都被摧毁了。所以我认为这种行为是设计的。

When I start the App and click on an item to get to the ItemDetailPage, and then go back, the instance is destroyed immediately. Then I navigate forward to ItemDetailPage again, and back again, and the instance stays undestroyed. I can even click other items to get to ItemDetailPages and back. All instances are kept alive. Then after some random time, all are destroyed. So I think this behaviour is by design.

我甚至考虑将所有摄像头代码移动到App.xaml.cs,因为我认为App类没有多个实例。但后来我无法得到正确的"可见"字样。用于设置其CaptureElement.Source的类。

I even thought about moving all camera code to the App.xaml.cs since I think there will be no multiple instanced of the App class. But then I can't get the correct "visible" class to set its CaptureElement.Source.

如果有多个页面实例,我应该如何处理Resume事件?或者如果它不应该有多个实例,为什么它会在我的项目中发生,更重要的是甚至在(几乎)未编辑的模板中?

How am I supposed to handle Resume events if there are multiple page instances? Or if it is not supposed to have multiple instances, why does it happen in my project and more importantly even in the (nearly) unedited template?

感谢您的帮助。

Thanks for any help.

推荐答案

那么,为什么不检查MediaCapture对象是否已经实例化我们导航到该页面以绕过此问题
Well, why not check if MediaCapture object has been instantiated we navigating to that page to bypass this issue


这篇关于[WP 8.1] [C#] Resume上有多个Page实例的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆