question

njsokalski avatar image
0 Votes"
njsokalski asked ·

Suspend & Resume for MediaCapture

I have a UWP app which uses a MediaCapture & CaptureElement to preview the camera. I am unsure what to do to handle suspending & resuming of the app to stop the camera when suspending & start it when resuming. Because the Suspending & Resuming events are part of the Application class (not the Page class), I cannot access my MediaCapture & CaptureElement from those handlers. What should I do?

windows-uwp
· 2
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hi, Sorry to bother you, did my reply solve your problem? Do you have any questions about this issue?

0 Votes 0 ·

Hello, is there any new information in your question, and can we provide you with new help?

0 Votes 0 ·

1 Answer

RichardZhang-MSFT avatar image
0 Votes"
RichardZhang-MSFT answered ·

Hello,​

Welcome to our Microsoft Q&A platform!

You have two ways to achieve The way to handle properties inside the page in the application lifecycle event.

(Take MainPage as an example)

1. Registering Application Lifecycle Events in a Page

 public MainPage()
 {
     this.InitializeComponent();
     Application.Current.Suspending += Current_Suspending;
     Application.Current.Resuming += Current_Resuming;
 }
    
 private void Current_Resuming(object sender, object e)
 {
     // Because it is a method registered in the Page, you can access the properties in the Page
 }
    
 private void Current_Suspending(object sender, Windows.ApplicationModel.SuspendingEventArgs e)
 {
     // Because it is a method registered in the Page, you can access the properties in the Page
 }

2. Create an accessible Page instance and access it in the App class

MainPage

 public static MainPage Current;
 public MainPage()
 {
     this.InitializeComponent();
     Current = this;
 }

App

 public App()
 {
     this.InitializeComponent();
     this.Suspending += App_Suspending;
     this.Resuming += App_Resuming;
 }
    
 private void App_Resuming(object sender, object e)
 {
     if (MainPage.Current != null)
     {
         var capture = MainPage.Current._capture;
         // do other things
     }
 }
 private void App_Suspending(object sender, SuspendingEventArgs e)
 {
     var deferral = e.SuspendingOperation.GetDeferral();
     if(MainPage.Current != null)
     {
         var capture = MainPage.Current._capture;
         // do other things
     }
     deferral.Complete();
 }


Thanks

· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.