C# – Metro Style App – Message Dialog

By | July 26, 2012

After the release date of Windows 8 i.e 26th October developers have gone crazy about developing metro style applications. I have been developing for windows earlier also and MessageBox was the most used control, but in metro style apps i didn’t found that, so here is a simply article which will guide you through the use of message box in metro style apps which are referred as Popups here. The message box here are fully user defined, we can add the commands to the message box and also add action along it. Here is a step wise instruction.

First step is to create a new Metro Style Blank App Project.

A blank project will open in front of you, we will add our code into ‘MainPage.xaml.cs‘, in the following project I will add the message box under the ‘OnNavigatedTo‘ event so that as soon as the Application is opened the message box will appear.

Now we will add a namespace for message box in metro style apps.

using Windows.UI.Popups;

Now its time to write down our actual code. Here is a code snippet.

First line create a new object for MessageDialog which is saved in a var md. MessageDialog has an argument which is the message box text which is displayed when the message box appears.

var md = new MessageDialog(“Are you sure you want to exit”);

Second line adds a new command to the message box i.e. ‘Yes’,  new UICommand creates an object for a new command and new UICommandInvokedHandler adds the action to the corresponding command, here the action we have added is: Application.Current.Exit(); this closes the application.

md.Commands.Add(new UICommand(“Yes”, (UICommandInvokedHandler) =>
{
Application.Current.Exit();
}));

Third line adds another command i.e. ‘No’. This will cause nothing to the application and the application will continue.

md.Commands.Add(new UICommand(“No”));

Debug the app to see the result.

Here is the source code. Enjoy.

Leave a Reply

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