Windows Phone Choosers with async/await

Currently, Windows Phone Choosers work is by invoking the Show method and then subscribing the Completed event and waiting for it to be invoked.

Wouldn’t it be great to just use await chooser.ShowAsync() and get the results immediately?

All choosers extend the ChooserBase class, and it’s exactly for this class that we are going to use to create a ShowAsync extension method.

We start by creating the extension method signature:

public static class ExtensionMethods
{
    public static Task<TTaskEventArgs> ShowAsync<TTaskEventArgs>(this ChooserBase<TTaskEventArgs> chooser)
        where TTaskEventArgs : TaskEventArgs
    {
    }
}

The async methods are required to return void, Task, or Task in order to be invoked with the await keyword. So our method will return a Task value, where the TTaskEventArgs generic type must be a TaskEventArgs subtype.

We will need to use a TaskCompletionSource object so we can return a Task and later on set the result of the asynchronous operation.

var taskCompletionSource = new TaskCompletionSource<TTaskEventArgs>();

Next we will add the code for the normal Chooser handling:

EventHandler<TTaskEventArgs> completed = null;

completed = (s, e) => {
    chooser.Completed -= completed;

    taskCompletionSource.SetResult(e);
};

chooser.Completed += completed;
chooser.Show();

Notice that we are removing the event handler after it gets invoked as not to have a memory leak! All that is missing now is just returning the Task object:

return taskCompletionSource.Task;

And that’s it!

Here’s how the full extension method should look:

public static class ExtensionMethods {
    public static Task<TTaskEventArgs> ShowAsync<TTaskEventArgs>(this ChooserBase<TTaskEventArgs> chooser)
        where TTaskEventArgs : TaskEventArgs
    {
        var taskCompletionSource = new TaskCompletionSource<TTaskEventArgs>();

        EventHandler<TTaskEventArgs> completed = null;

        completed = (s, e) => {
            chooser.Completed -= completed;

            taskCompletionSource.SetResult(e);
        };

        chooser.Completed += completed;
        chooser.Show();

        return taskCompletionSource.Task;
    }
}

You can download a sample code for this directly from Microsoft Code Gallery:

Async Chooser Sample (external link)
Downloaded 533 times

6 thoughts on “Windows Phone Choosers with async/await

  1. To ensure that your application receives the result of the Chooser task when your application is reactivated, the Chooser object must be declared with a global scope within the T:Microsoft.Phone.Controls.PhoneApplicationPage class and you must initialize the Chooser and assign the E:Microsoft.Phone.Tasks.ChooserBase`1.Completed event delegate in the T:Microsoft.Phone.Controls.PhoneApplicationPage constructor. For more information about how to handle the activation and deactivation of your application, see The Windows Phone Task Model.

    http://msdn.microsoft.com/en-us/library/windowsphone/develop/ff707989(v=vs.105).aspx

Leave a Reply