1

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?

irodrisa
  • 57
  • 1
  • 7
  • The app behaves different, because when VS is attached, the *Suspending/Resuming* [events are not being called](http://stackoverflow.com/q/25532748/2681948). So if you have some code that can crash the app while it's being suspended, then it will behave just like you have said - with VS it will run fine, but normally it will crash. To test *Suspending/Resuming*, you will have to [invoke it manually](http://stackoverflow.com/a/24103734/2681948). – Romasz Sep 10 '14 at 06:29
  • Just to be clear, I did manually invoke Suspend/Resume from Visual Studio, as you indicate @Romasz. The behavior of the app is correct when doing so, but incorrect when executing normally, without VS attached that is. Any other ideas? – irodrisa Sep 10 '14 at 16:32
  • If you comment out *StartCameraPreview* and *StopCamperaPreview* and you run file picker - is the exception being thrown? Maybe you can share a sample project which reproduces the issue? – Romasz Sep 10 '14 at 16:59
  • I did try commenting out those two calls: no exception is thrown while debugging, but again, when executing without VS, the app crashes. The whole project is available here: http://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. – irodrisa Sep 10 '14 at 23:31

1 Answers1

0

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.

Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Thanks Romasz, moving the FilePicker out of the constructor fixed the crash. However, there seems to be still a race condition if I navigate to a page after retrieving the file from the FilePicker. I've updated the app [here](https://onedrive.live.com/redir?resid=8BBE80468A185943!70189&authkey=!AIhqiX75DwXjufQ&ithint=file%2czip). The repro steps are: choose Pick photo, and then select a picture. The race condition has 2 different manifestations: (Part I of II) – irodrisa Sep 12 '14 at 00:08
  • 1. Sometimes, the app stays on the MainPage after selecting a picture (it looks like it's actually navigating to the LoadPhoto page but something makes the Frame go back to the MainPage). 2. Other times, the app successfully navigates to the LoadPhoto page and renders the image, but if I navigate back with the Back button and then press the Pick photo button again, the FilePicker is briefly shown and then the app crashes. This behavior doesn't repro with VS attached. Everything works fine while executing in Debug mode. (Part II of II) – irodrisa Sep 12 '14 at 00:08
  • @irodrisa Try to debug suspend/resume - invoke them by *Lifecycle events* manually. Unfortunately I have much work for now and next week. After that if you still have some problems, give me a sign. – Romasz Sep 12 '14 at 08:18
  • I think I've figured out what the issue is: the `ContinueFileOpenPicker` code is executed from a worker thread, so I shouldn't call `this.Frame.Navigate(typeof(LoadPhoto), file);` from that thread. That call should be made from the main thread, but I'm not sure how to do that. I'll keep researching. – irodrisa Sep 12 '14 at 20:38
  • Unfortunately, this doesn't work: `await CoreWindow.GetForCurrentThread().Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { this.Frame.Navigate(typeof(LoadPhoto), file); });` – irodrisa Sep 12 '14 at 21:06
  • Romasz, I've marked your answer as valid since it did fix the original crash. I've posted the follow up question above as a new question [here](http://stackoverflow.com/questions/25817100/navigate-to-a-different-page-from-continuefileopenpicker-method). Thanks a lot for your help! – irodrisa Sep 12 '14 at 21:28