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 SetResultfunction 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, ...

Get Xamarin Blueprints now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.