Suspend & Resume for MediaCapture

Nathan Sokalski 4,116 Reputation points
2019-12-05T01:44:51.187+00:00

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?

Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Richard Zhang-MSFT 6,936 Reputation points
    2019-12-05T02:15:47.147+00:00

    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

    0 comments No comments