The TaskCompletionSource framework
The ShowAlert
function will instantiate a new instance of a TaskCompletionSource
of type bool
. We then invoke the action on the main thread using the UIApplication.SharedApplication
, and then return the Task
object of the TaskCompletionSource
. This means we can wait for the task to be returned When we create the UIAlertView
, we set the Clicked
event of the dialog to call the SetResult
function of the TaskCompletionSource
, so the Task
will not finish until this click event has occurred:
public Task<bool> ShowAlert(string title, string message) { var tcs = new TaskCompletionSource<bool>(); UIApplication.SharedApplication.InvokeOnMainThread(new Action(() => { UIAlertView alert = new UIAlertView(title, message, null, NSBundle.MainBundle.LocalizedString("Cancel", "Cancel"), NSBundle.MainBundle.LocalizedString...