Opening message boxes

Opening message boxes

[This article is for Windows 8.x and Windows Phone 8.x developers writing Windows Runtime apps. If you’re developing for Windows 10, see the latest documentation]

Open a UIAlertView-style pop-up box in Windows 8 and Windows Phone 8.1, using the MessageDialog control.

If you need to warn or inform your user of something important – so important you are willing to break the general flow of your app – then the MessageDialog control is for you. When writing apps in iOS, you will probably have used the UIAlertView control; in Windows use MessageDialog. Here are some short code samples to display the simplest possible dialog box.

The default message box control.

For more detailed examples which include adding multiple buttons, please see the MessageDialog class topic.

Showing a MessageDialog in C#

When opening a MessageDialog, note that the call to display the message dialog must be called from inside a method classed as async. Here's a sample method:


private async void ShowMessage()
        {
            // using Windows.UI.Popups;
            MessageDialog messageDialog = new MessageDialog("Hello, World!");
            await messageDialog.ShowAsync();
        }

Showing a MessageDialog in JavaScript

When coding in JavaScript, use a function like this:


function ShowMessage() {
        var messageDialog = new Windows.UI.Popups.MessageDialog("Hello, World!");
        messageDialog.showAsync();
    };


Showing a MessageDialog in C++

When using C++, use a function like this:


void ShowMessage()
{
	// using namespace Windows::UI::Popups;
	MessageDialog ^messageDialog = ref new MessageDialog("Hello, World!");
	messageDialog->ShowAsync();
}


Multiple choice MessageDialog controls

The MessageDialog control can be configured with multiple buttons, all of which can trigger a unique action.


        private async void ShowMessage()
        {

            MessageDialog messageDialog = new MessageDialog("What is your favorite color?");
   
            messageDialog.Commands.Add(new UICommand(
               "None, I'm color blind.",
               new UICommandInvokedHandler(this.CommandInvokedHandler_none)));
 
            messageDialog.Commands.Add(new UICommand(
                "Blue",
                new UICommandInvokedHandler(this.CommandInvokedHandler_blue)));

            messageDialog.Commands.Add(new UICommand(
                "Blue, no red!",

               new UICommandInvokedHandler(this.CommandInvokedHandler_red)));
            await messageDialog.ShowAsync();
        }

        private void CommandInvokedHandler_none(IUICommand command)
        {
            // option 1 - the default
        }
        private void CommandInvokedHandler_blue(IUICommand command)
        {
            // option 2
        }

        private void CommandInvokedHandler_red(IUICommand command)
        {
            // option 3
        }



You can set up a separate handler for each response (as above) or use the same handler, and query the command.Label property to see which option was selected.

Related topics

MessageDialog class

 

 

Show:
© 2017 Microsoft