C#

A Custom (Async) MessageBox for WPF and Windows Phone

The System.Windows.MessageBox provided by .NET is limited to a very small set of MessageBoxButtons (Yes/No/OK/Cancel) which do not allow for custom Button texts. The System.Windows.Forms.MessageBox also blocks the UI thread, which can be an annoying disadvantage as well.

The MessageBox on Windows Phone has the same disadvantages.

Custom buttons in a MessageBoxe simplify the decision for the user, because they can describe the results of the action:

Use your own buttons and await the result!
Use your own buttons and await the result!
This MessageBox does not block the UI thread!
This MessageBox does not block the UI thread!

However you need to do this with a custom control, so for WPF as well as Windows Phone I designed a simple method for asynchronously showing a MessageBox with custom buttons. (The WPF implementation based on this control by Evan Wondrasek)

So let’s bring some sample code.In WPF you can use multiple custom buttons:

asynchronously show a CustomMessageBox - int result is the number of the selected button, or the default
asynchronously show a CustomMessageBox – int result is the number of the selected button, or the default

And on Windows Phone you’re limited to 2 buttons:

asynchronously show a CustomMessageBox - int result is the number of the selected button, or the default
asynchronously show a CustomMessageBox – int result is the number of the selected button, or the default

To use CustomMessageBox, you need to get TCD.Controls from NuGet:

Install the TCD.Controls package from NuGet
Install the TCD.Controls package from NuGet

And on Windows Phone 7.x you also need async support, which is included in the Microsoft.Bcl.Async package:

Microsoft.Bcl.Async is required for async/await on WP7
Microsoft.Bcl.Async is required for async/await on WP7

Just like the other methods from TCD.Controls, CustomMessageBox also features IntelliSense descriptions (or I’ll add them very soon).

Please feel free to provide feedback and/or report bugs 😉

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.