Lifecycle / Navigation Management MediaCapture + CaptureElement

This application is for the Windows Phone 8.1 Store. My MainPage has a CaptureElement to display a preview stream from my MediaCapture object. For navigation in the application (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 go to other pages and return, and the preview is reliable. However, I ran into problems for the following scenarios:

  • The user clicks the Windows button, then the back button
  • The user clicks the Windows button, then uses the task switch to return to my application.
  • The user presses the search button, then the back button
  • The user presses the power button, then presses it again and searches to unlock the device.
  • The user holds the back button to enter the task switch, then clicks on my application again.

After each of the above actions (and / or combinations thereof), when my application returns, the preview is frozen in the last frame displayed.

If the user then goes to another page and then returns to MainPage, the preview starts up again without any problems, so this leads me to think that I just need to stop / start the preview after returning from one of the above scripts.

I tried to subscribe to the App.Suspending and App.Resuming , but in these cases they do not fire. What am I missing?

+6
source share
2 answers

You will need to use App.Suspending and App.Resuming (for the cases described) with a combination of navigation events (when navigating between pages). The OnNavigatingFrom event is OnNavigatingFrom when you press Start , hold Back or use Search (when the application pauses), but when you resume the application, OnNavigatedTo not OnNavigatedTo - this event is OnNavigatedTo only during navigation. Therefore, in your case, when you click Start , the preview will stop, and when you return, it will not start again. Unsubscribe from MSDN :

Note. On Windows Phone, OnNavigatedFrom () is called when the application is paused. OnNavigatedTo () is not called when the application resumes.

Another thing is that in order to properly debug the application, you will need to use the Lifecycle Events event on the "Debug location" tab in Visual Studio - until you debut in the application, it does not pause, but when you start the application in normal mode, it pauses immediately after clicking Start .

Please note that the application can be placed in the "Not working" state. Read more about Life Cycle on MSDN .

+4
source

The scripts you described should fire the Window.Current.VisibilityChanged event, where you can use the VisibilityChangedEventArgs.Visible passed to the event handler to preview when it is not displayed, and to initialize the preview when it is visible. You can subscribe \ unsubscribe to the Window.Current.VisibilityChanged event in the Loaded \ Unloaded handler for your \ UserControl page.

The reason the Suspend / Resume life cycle events are insufficient is because the scenarios described above do not deterministically trigger these events at a specific time, since the OS pauses the application only based on internal policies that may change from the OS release updates.

As well as on the sidelines, I would avoid using navigation handlers and instead rely on Loaded \ Unloaded handlers to let you initialize \ cleanup properly if you ever had to move your CaptureElement to your own UserControl, and not on the page and avoids the scenario where WP will call OnNavigatedFrom and not call OnNavigatedTo for suspend \ resume (Loaded \ Unloaded will always be called in order).

+1
source

Source: https://habr.com/ru/post/970126/


All Articles