Skip to content

Instantly share code, notes, and snippets.

@programmation
Created July 28, 2015 01:21
Show Gist options
  • Save programmation/47290a3b97867bbffabc to your computer and use it in GitHub Desktop.
Save programmation/47290a3b97867bbffabc to your computer and use it in GitHub Desktop.
Xamarin Forms page returning a result
// http://forums.xamarin.com/discussion/comment/129611/#Comment_129611
public interface IPageWithResult<out T>
{
T PageResult { get; }
}
public static class PageExtensions
{
public static Task<TResult> ShowModalAsync<TPage, TResult>(this TPage page)
where TPage : Page, IPageWithResult<TResult>
{
var tcs = new TaskCompletionSource<TResult>();
System.EventHandler<ModalPoppedEventArgs> handler = null;
handler = (_, args) =>
{
if (args.Modal == page)
{
Application.Current.ModalPopped -= handler;
tcs.SetResult(page.PageResult);
}
};
Application.Current.ModalPopped += handler;
Application.Current.MainPage.Navigation.PushModalAsync(page);
return tcs.Task;
}
}
// Usage:
public partial class Page2 : IPageWithResult<string>
var modalPage = new Page2();
string result = await modalPage.ShowModalAsync<Page2, string>();
Debug.WriteLine(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment