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

1k Views Asked by At

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?

1

There are 1 best solutions below

6
On BEST ANSWER

The problem exists because you are running your FileOpenPicker in the constructor of your Page. That's nothing good. To test, I've provided a button in your LoadPhoto page:

In XAML:

<Grid>
    <Button Name="myButton" Content="Click me"/>
    <Image x:Name="Image" Stretch="Uniform"/>
</Grid>

In constructor:

public LoadPhoto()
{
    this.InitializeComponent();
    this.navigationHelper = new NavigationHelper(this);
    myButton.Click += (sender, e) => LaunchPicker();
}

The code you can download here.

Maybe better you can first pick a file, then navigate to a page.