Skip to content

Instantly share code, notes, and snippets.

@hjerpbakk
Last active August 29, 2015 14:10
Show Gist options
  • Save hjerpbakk/622e5855f95189e13d77 to your computer and use it in GitHub Desktop.
Save hjerpbakk/622e5855f95189e13d77 to your computer and use it in GitHub Desktop.
Using a TaskCompletionSource to enable async and await together with UIAlertController. From: http://www.hjerpbakk.com/blog/2014/11/23/from-uialertview-to-uialertcontroller-using-xamarin-and-async-await
public static class CustomerFeelingSheet {
public static Task<CustomerFeeling> ShowRatingDialogAsync(UIViewController parent) {
var taskCompletionSource = new TaskCompletionSource<CustomerFeeling>();
var alert = UIAlertController.Create("howDoYouFeel".T(), null, UIAlertControllerStyle.ActionSheet);
alert.AddAction(UIAlertAction.Create("likeIt".T(), UIAlertActionStyle.Default,
a => taskCompletionSource.SetResult(CustomerFeeling.LikeIt)));
alert.AddAction(UIAlertAction.Create("couldBeBetter".T(), UIAlertActionStyle.Default,
a => taskCompletionSource.SetResult(CustomerFeeling.CouldBeBetter)));
alert.AddAction(UIAlertAction.Create("cancel".T(), UIAlertActionStyle.Cancel,
a => taskCompletionSource.SetResult(CustomerFeeling.DontCare)));
parent.PresentViewController(alert, true, null);
return taskCompletionSource.Task;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment