C# – MessageDialog class

When developing apps for Windows RT, you might sometimes want to show a dialog messagebox. This can be to just display a message to the user or to show debugging information. A messagebox should however not be the first go-to method when debugging.

We will be using the MessageDialog class which is available for Windows RT apps.

The basic use of it is like the following:

<pre>var msg = new MessageDialog(_msg);
msg.ShowAsync();</pre>

Where you should remember to put the following statement at the top of your file:

using Windows.UI.Popups;

Using it like this, you might however end up in trouble, if your code ends up trying to display multiple messages at the same time, as this will throw an error saying that access to the dialog is restricted.

To circumvent this, we are using the following static class:

using System;
using System.Collections.Generic;
using System.Text;
using Windows.Foundation;
using Windows.UI.Popups;

namespace AppShare
{
    public static class Shout
    {

        private static IAsyncOperation<IUICommand> asyncCommand = null;

        public static void Dialog(string _msg){
            if (asyncCommand != null)
            {
                asyncCommand.Cancel();
            }
            var msg = new MessageDialog(_msg);
            asyncCommand = msg.ShowAsync();
        }

    }
}

It basically stores the dialog box in a variable. Before opening the dialog, it closes the exiting one, if it is open. It can be used like the following:

AppShare.Shout.Dialog("My message to display!");

 

bb@morningtrain.dk'

Bjarne Bonde