Xamarin.Android await AlertDialog.Builder

Sometimes you need to present the user with data and wait for a response all while not lock up the UI thread. Microsoft’s asynchronous programming model our best friend and in just a few lines we can wait for a response from DialogBuilder. In this code example we’ll wait for AlertDialog’s positive or negative button being tapped.

This AlertAsync method uses the TaskCompletionSource<TResult> type to create the task and handle the completion. Upon the user tapping the positive or negative button we call TrySetResult() on the TaskCompletionSource to return our bool value.

Image for post
Image for post
AlertDialog

Happy Coding!

Written by

Principal Software Engineer at Command Health https://github.com/dannycabrera

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store