Xamarin.Android await AlertDialog.Builder

Danny Cabrera
Jun 14, 2018 · 1 min read

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.

AlertDialog

Happy Coding!

Danny Cabrera

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

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade