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
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 Taskawait keyword. So our method will return a Task
We will need to use a TaskCompletionSource
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:
Pingback: Windows Phone Choosers with async/await
Pingback: windows phone choosers with async/await
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
That is actually a very good point, thanks for bringing that out!
I’ll investigate this further and see if there is anything I can do to mitigate the issue.
How i can solve this issue
http://stackoverflow.com/questions/14621101/issue-with-async-await-method-along-with-choosers-and-launchers
Looking like it is not working on WP7 with https://nuget.org/packages/Microsoft.Bcl.Async/
Debugger shows, that next line after
var photoResult = await new PhotoChooserTask().ShowAsync();
in WP7 is running imideately, but in WP8 it waits until task is finished.