问题描述
这是Windows Phone 8.1应用商店中的应用程序.我的 MainPage
有一个 CaptureElement
,用于显示来自我的 MediaCapture
对象的预览流.要在应用程序内(页面之间)导航,效果很好:
This is in a Windows Phone 8.1 Store app. My MainPage
has a CaptureElement
to display the preview stream from my MediaCapture
object. For navigation within the app (between pages), this works well:
MediaCapture mc;
protected override async void OnNavigatedTo(NavigationEventArgs e)
{
mc = new MediaCapture();
await mc.InitializeAsync();
preview.Source = mc;
await mc.StartPreviewAsync();
}
protected override async void OnNavigatedFrom(NavigationEventArgs e)
{
await mc.StopPreviewAsync();
}
我可以导航到其他页面并返回,预览运行可靠.但是,在以下情况下我遇到了问题:
I can navigate to other pages and come back, and the preview runs reliably. I'm running into problems for the following scenarios though:
- 用户先按Windows按钮,然后按后退按钮
- 用户按下Windows按钮,然后使用任务切换器返回我的应用程序
- 用户先按下搜索按钮,然后再按下后退按钮
- 用户按下电源按钮,然后再次按下并向上滑动以解锁设备
- 用户按住后退按钮进入任务切换器,然后再次点击我的应用程序
上述每个操作(和/或它们的组合)之后,当我的应用返回时,预览将冻结在显示的最后一帧.
After each of the above actions (and/or combinations of them), when my app comes back the preview is frozen at the last frame that was displayed.
如果用户随后导航到另一个页面,然后返回到MainPage,则预览将再次开始运行而没有任何问题,因此这使我相信,从其中一个返回后,我只需要停止/启动预览.以上情况.
If the user then navigates to a different page and then back to the MainPage, the preview starts running again without an issue, so this leads me to believe I just need to stop/start the preview after coming back from one of the above scenarios.
我尝试订阅了 App.Suspending
和 App.Resuming
事件,但是这些事件不会在这些情况下触发.我想念什么?
I tried subscribing to the App.Suspending
and App.Resuming
events, but these don't trigger on these occasions. What am I missing?
推荐答案
对于您描述的情况,您将必须使用 App.Suspending
和 App.Resuming
)与导航事件的组合(在页面之间导航时).当您按下,按住或使用时(当该应用被暂停时,会调用 OnNavigatingFrom
事件),但是当您恢复该应用程序时,不会被调用 OnNavigatedTo
-仅在导航时才调用此事件.因此,根据您的情况,当您点击时,预览会停止,而当您返回时,预览不会再次开始.对MSDN的引用:
You will have to use App.Suspending
and App.Resuming
(for the cases you have described) with combination of Navigation events (when navigating between Pages). The OnNavigatingFrom
event is called when you hit , hold or use (when the App is being suspended), but when you resume the App, OnNavigatedTo
is not being called - this event is called only when you are navigating. So in your case, when you hit , the preview stops and when you come back it doesn't start again. A refference to MSDN:
另一件事是,要正确调试该应用程序,必须在Visual Studio中使用 Debug Location选项卡的 Lifecycle Events -在调试该应用程序时,它会不会被暂停,但是当您正常运行您的应用程序时,在您按后,它就会被暂停.
The other thing is that to debug the App properly you will have to use Lifecycle Events of Debug Location tab in Visual Studio - while you are debbuging the app, it is not being suspended, but when you run your app normally, it gets suspended just after you hit .
还请注意,该应用程序可以置于未运行状态.有关 MSDN上的生命周期的信息.
Note also that the App can be put into Not Running state. More about Lifecycle at MSDN.
这篇关于MediaCapture + CaptureElement生命周期/导航管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!