Detecting whether or not Zune is connected when using photochooser task.

When a windows phone is connected to the computer and zune is open, apps on the phone can’t access the photos stored on the phone through the photochooser task. If zune is connected the photochooser task will immediately return with e.TaskResult set to TaskResult.Cancel, which is the same return value as if the user had decided not to pick a photo and just clicked the back button. This presents a problem when your app relies on the user picking an image and you need to know if the user changed their mind or needs to disconnect their phone.

There is a difference in the way the photochooser tasks returns when connected, other than returning extremely quickly. Normally when PhotoChooserTask.Show() is called, it fires off the standard navigation events. Then if the chooser exits without the user selecting a photo it returns with TaskResult.Cancel. However, if zune is connected, the navigation events are not fired and the task immediately return with TaskResult.Cancel.

What we can do is create a class level boolean which we use to determine if the picture chooser has been shown

private bool photoChooserShown;

Immediately before the picture chooser is called set it to false

photoChooserShown = false;
photoChooserTask.Show();

Then in the OnNavigatingFrom method if the URI of the page being navigated is outside of our app, then the picture chooser is being shown,

protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
        {
            if (e.Uri.ToString() == "app://external/")
                photoChooserShown = true;
            base.OnNavigatingFrom(e);
        }

Finally, in the chooser task completed method, we can test to see if the app has navigated away from since we called the show method

void photoChooserTask_Completed(object sender, PhotoResult e)
{
    if(photoChooserShown)
    {
        //zune not connected
    }
    else
    {
        //zune connected
    }
}
This entry was posted in wp7Dev and tagged , , , , . Bookmark the permalink. Both comments and trackbacks are currently closed.