Reputation: 379
I have app with multiple pages, connected with navigateto logic.
One of pages contains media element with webcam binding. After entering to background mode (for example, by minimizing app thought clicking system menu), camera element stopped. So, I subscribe to
Windows.ApplicationModel.Core.CoreApplication.LeavingBackground event and reinitialize camera. Everything is ok if current page is page with this subscription and camera element. If current page is another page, and app is restored, LeavingBackground this event occurs anyway, so hidden page trying to reinitialize camera.
I tried to set this.NavigationCacheMode = NavigationCacheMode.Disabled, so instance of page that contains media element and subscription to LeavingBackground event theoretically have to be disposed after NavigatedTo event according MSDN. But it's work other way that I'm not understand.
It seems that camera page instantiated once, and forever, and will receive LeavingBackgound event always - that's bad for me.
I tried to compare Window.Current.Content.GetType() with type of page that contains camera element, but sometimes this type contains type of another pages, but sometimes it shifted with Content.Content, so I'm stuck.
Upvotes: 3
Views: 2770
Reputation: 13850
You need to handle the Suspending and Resuming events to clean-up and re-initialize the camera correctly, like it is shown in the camera sample app:
Thanks, Stefan Wick - Windows Developer Platform
Upvotes: 1
Reputation: 6091
I would assume you have to unregister the event handler when navigating away from that page:
public sealed partial class WebCamPage
{
public WebCamPage()
{
InitializeComponent();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
Windows.ApplicationModel.Core.CoreApplication.LeavingBackground += OnLeavingBackground;
}
protected override void OnNavigatingFrom(NavigatingCancelEventArgs e)
{
Windows.ApplicationModel.Core.CoreApplication.LeavingBackground -= OnLeavingBackground;
}
private void OnLeavingBackground(object sender, LeavingBackgroundEventArgs e)
{
// Your code here.
}
}
Upvotes: 3