Created
July 28, 2015 01:21
-
-
Save programmation/47290a3b97867bbffabc to your computer and use it in GitHub Desktop.
Xamarin Forms page returning a result
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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