Edit 2001/10/10: I noticed that I actually get InvalidOperationException on Mango (seems like all the time now, but I’m not 100% sure). Updated code in CaptureTask_Completed to check for this.
Rene had a great blog post about How to detect the Zune software about an year ago. Unfortunately, the method he describes no longer works in Mango.
Moreover, when you get the Cancel event from the PhotoChooserTask in Mango, neither OnNavigatingFrom nor OnNavigatedTo is called for your page.
This can cause crashes, grief and unhappy customers who think “I can’t open a picture from this app”.
Here is my current solution that works for NoDo and Mango. It’s very simple, I just check if the request to open the picture happened less than 1 second after the Cancel event occurred.
In real life testing (you should have seen me frantically hitting the back button!), one second seems to work great.
Here’s some code to illustrate this:
private void btnOpenFromPhone_Click(object sender, RoutedEventArgs e)
{
if (Globals.PhotoChooserTask == null)
{
Globals.PhotoChooserTask = new PhotoChooserTask();
}
Globals.PhotoChooserTask.Completed += CaptureTask_Completed;
Globals.PhotoChooserTask.ShowCamera = true;
try
{
Globals.PhotoChooserStarted = DateTime.Now;
Globals.PhotoChooserTask.Show();
}
catch (InvalidOperationException) // will be thrown in case we double-tap and other navigation is in progress
{
}
}
void CaptureTask_Completed(object sender, PhotoResult e)
{
if (e.Error != null)
{
if (e.Error is InvalidOperationException)
{
MessageBox.Show("The Zune software is running and prevents opening pictures. Please close Zune or disconnect the phone from your computer and try again.", "Information", MessageBoxButton.OK);
}
return;
}
if (e.TaskResult == TaskResult.OK)
{
// yay we’re done!
return;
}
if (e.Error is InvalidOperationException)
{
throw e.Error;
}
if (e.TaskResult == TaskResult.Cancel)
{
AvailableTools.Brush.SetImage(Globals.MainImage); // set the image back to the brush again, because in case of Zune connected to PC OnNavigatingFrom and OnNavigatedTo will not be called!
if ((DateTime.Now - Globals.PhotoChooserStarted) < TimeSpan.FromSeconds(1)) // returned too fast, assume Zune is causing problems
{
MessageBox.Show("The Zune software is running and prevents opening pictures. Please close Zune or disconnect the phone from your computer and try again.", "Information", MessageBoxButton.OK);
}
}
}
If anyone knows a better solution, please let me know!