Skip to content

Instantly share code, notes, and snippets.

@dannycabrera
Last active December 25, 2015 13:09
Show Gist options
  • Save dannycabrera/6981542 to your computer and use it in GitHub Desktop.
Save dannycabrera/6981542 to your computer and use it in GitHub Desktop.
Xamarin.iOS Awaitable ShowAlert From: http://tirania.org/monomac/archive/2013/Oct-03.html
async void Demo ()
{
switch (await ShowAlert ("Title", "Message", "Ok", "Cancel")){
case 0: // Ok
....
case 1: // Cancel
....
}
}
// Displays a UIAlertView and returns the index of the button pressed.
public static Task<int> ShowAlert (string title,
string message,
params string [] buttons)
{
var tcs = new TaskCompletionSource<int> ();
var alert = new UIAlertView {
Title = title,
Message = message
};
foreach (var button in buttons)
alert.AddButton (button);
alert.Clicked += (s, e) => tcs.TrySetResult (e.ButtonIndex);
alert.Show ();
return tcs.Task;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment