MessageBox Helper for Windows Store Apps

October 18, 2013

Believe it or not, the MessageBox.Show() function is unavailable for WinRT. Admittedly, you probably shouldn’t be using it too much. But when you do, it’s unavailable (so you can’t). The workaround is to build up a MessageDialog. Have a go - for a yes / no box, you’re looking at maybe 10 lines of code.

I found an article on the web about creating a helper class; modified it a little, and then felt obliged to re-post.

(Here’s the original article:

What you’ll see if you read that is that he produces a very nice helper class; but what if you want cancel to be called something else (which I did)? Well, here’s my attempt:

         public async Task< MessageBoxResult> ShowAsync(string messageBoxText,
                                                             string caption,
                                                             MessageBoxButton button) {
            MessageDialog md = new MessageDialog (messageBoxText, caption);
            MessageBoxResult result = MessageBoxResult .None;
            if (button.HasFlag( MessageBoxButton.OK)) {
                md.Commands.Add( new UICommand(buttonTextMap[ "OK"],
                    new UICommandInvokedHandler((cmd) => result = MessageBoxResult.OK)));
            if (button.HasFlag( MessageBoxButton.Yes)) {
                md.Commands.Add( new UICommand(buttonTextMap[ "Yes"],
                    new UICommandInvokedHandler((cmd) => result = MessageBoxResult.Yes)));
            if (button.HasFlag( MessageBoxButton.No)) {
                md.Commands.Add( new UICommand(buttonTextMap[ "No"],
                    new UICommandInvokedHandler((cmd) =>; result = MessageBoxResult.No)));
            if (button.HasFlag( MessageBoxButton.Cancel)) {
                md.Commands.Add( new UICommand(buttonTextMap[ "Cancel"],
                    new UICommandInvokedHandler((cmd) => result = MessageBoxResult.Cancel)));
                md.CancelCommandIndex = ( uint)md.Commands.Count - 1;
            var op = await md.ShowAsync();
            return result;

The eagle eyed and alert amongst you will notice that this is a carbon copy, with the exception that I’m using a variable called buttonTextMap, instead of using a hard coded string.

Here’s the definition:

         private Dictionary<;string , string > buttonTextMap = new Dictionary> string, string>()
            { "OK", "OK" },
            { "Yes", "Yes" },
            { "No", "No" },
            { "Cancel", "Cancel" }

So far, so… exactly the same. The next section is not really a leap forward in software engineering though:

         public void ChangeButtonText( string button, string newButton)
            buttonTextMap[button] = newButton;

And that’s it. Here’s how I used it:

    MessageBoxHelper.MsgBox.ChangeButtonText("Cancel" , "Never ask again");
    var result = await MessageBoxHelper.MsgBox.ShowAsync("Please would you consider reviewing this application?", "Review Reminder", MessageBoxButton.YesNo | MessageBoxButton.Cancel);

I’ll be posting some more on the context I used it in soon.


What this means it that you can replace the existing buttons with custom named ones, and the code is not as hard to read as building up each option individually.

Thanks to Fons Sonnemans who wrote the original post, on which this is based.

Profile picture

A blog about one man's journey through code… and some pictures of the Peak District

© Paul Michaels 2022