Chooser.ShowAsync()

public static Task<T> ShowAsync<T>(this ChooserBase<T> chooser)
    where T : TaskEventArgs {

    TaskCompletionSource<T> tcs = new TaskCompletionSource<T>();

    EventHandler<T> handler = null;

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

        if (e.Error != null)
            tcs.SetException(e.Error);
        else
            tcs.SetResult(e);
    };

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

    return tcs.Task;
}
Example:
private async void Button_Click(object sender, RoutedEventArgs e) {
    try {
        var c = new PhotoChooserTask();
        var result = await c.ShowAsync();
        var bi = new BitmapImage();
        bi.SetSource(result.ChosenPhoto);
        imagePhoto.Source = bi;
    } catch (Exception ex) {
        MessageBox.Show(ex.Message);
    }
}

Description

Show Windows Phone Choosers using the async/await pattern instead of using the completed event.

Details

Double click on the code to select all.

 

;