locked
Handling Resuming event for CaptureElement when combined with the File Picker sample

    Question

  • This applies to a Windows Universal App created from the File Picker sample. The base code for that sample includes the ContinuationManager class in the Windows Phone project and the OnActivated method in the App.xaml.cs file, as well as a common NavigationHelper class.

    I'm also using MediaCapture and CaptureElement in the solution but I'm failing to properly deal with the Resuming event. This is what I do:

    I use the NavigationHelper_LoadState and NavigationHelper_SaveState methods in order to start and stop the camera preview (this is part of the LiveCamera.xaml.cs file).

    private async void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    {
        // Start the camera preview
        await StartCameraPreview();
    }
    
    private async void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    {
        // Stop the camera preview
        await StopCameraPreview();
    }

    This works well when navigating between pages inside the app, but doesn't stop/restart the camera on Suspend/Resume events.

    I fixed this by adding to App.xaml.cs the following method to handle the Resuming event (the SuspensionManager takes care of calling the NavigationHelper_LoadState method upon Resuming the app):

    async void App_Resuming(object sender, object e)
    {
        await SuspensionManager.RestoreAsync();
    }

    The code above works well when executed with Visual Studio attached (both in Debug and Release mode): the camera preview stops/restarts when receiving Suspend/Resume events and the File Picker properly returns a file.

    However, if I execute the app without Visual Studio (simply launching the app from the app list), the camera preview still stops/restarts when receiving Suspend/Resume events but when choosing a file with the File Picker, I see the "Resuming..." progress bar and then the app simply crashes.

    Somehow the App_Resuming and OnActivated methods collide after choosing a file. I've verified this by showing a MessageDialog when entering each method (since I cannot repro the issue with Visual Studio): after I choose a picture, I briefly see the App_Resuming message right before the app crash (never get to see the OnActivated message). I was not expecting that method to be called after the File Picker since that method doesn't ever get called when executing the app with VS attached.

    Why are different (and from what I understand, incorrect) methods being called when VS is not attached?

    Just to be clear, I did manually invoke Suspend/Resume from Visual Studio. The behavior of the app is correct when doing so, but incorrect when executing normally, without VS attached that is. I tried commenting out StartCameraPreview and StopCamperaPreview: no exception is thrown while debugging, but again, when executing without VS, the app crashes.

    The whole project is available here: 1drv.ms/1rJvq8z The repro steps are simple: launch the app, click on Pick photo and choose any photo (you can even take a new photo in the emulator). After that, the app crashes instead of returning the photo. Try those same repro steps with VS attached, and everything will work fine.


    • Edited by irodrisa Thursday, September 11, 2014 1:28 AM
    Thursday, September 11, 2014 12:09 AM

Answers

All replies

  • I tried this with and without Visual Studio attached - I did not experience a crash.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Thursday, September 11, 2014 7:43 PM
    Moderator
  • Thanks for trying. I posted the same question in StackOverflow and they were able to repro the issue.
    The root cause was having the FileOpenPicker in the constructor of the Page.

    See all the details here: http://stackoverflow.com/questions/25754997/handling-resuming-event-for-mediacapture-captureelement-when-combined-with-the

    • Marked as answer by irodrisa Friday, September 12, 2014 12:43 AM
    Friday, September 12, 2014 12:43 AM