Custom MessageBox in Universal Windows Platform (UWP)


For Universal Apps, the new APIs require you to use await MessageDialog().ShowAsync() (in Windows.UI.Popups) to bring it into line with Win 8.1.

var dialog = new MessageDialog("Your message here");
await dialog.ShowAsync();

The above code produces the result shown below:


You can go ahead to add your custom commands such as:

dialog.Title = "Delete?";
dialog.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
dialog.Commands.Add(new UICommand { Label = "No", Id = 1 });

The above implementation of message box cannot be customized to fit into your theme without really getting your hands dirty and if you don’t mind you can go ahead.

A cheap way to quickly display a custom message box in your app is to use the ContentDialog in the Universal Windows template.

ContentDialog class represents a dialog box that can be customized to contain checkboxes, hyperlinks, buttons and any other XAML content. Read more here.

To add ContentDialog, Right-Click on your root project folder, Add -> New Item -> ContentDialog, give it a name and add to your project.


Then you have this:


Your XAML code will look like this:



The next thing is to remove the following part from your XAML code:


The trick here starts by removing the default buttons and adding your own buttons (“OK” & “Cancel”) to the ContentDialog. You can style these buttons as you like and use labels apart from “OK” & “Cancel”.

<StackPanel Orientation="Horizontal">
<Button x:Name="OkBtn" Content="OK"></Button>
<Button x:Name="CancelBtn" Content="Cancel" Margin="10,0,0,0"></Button>

Next, we want to handle the click events of our buttons, this is where we will set the result of the dialog. The click events of your buttons should look like this:

 public sealed partial class MessageDisplay : ContentDialog
 public MessageDisplay(string message, string title)

 this.Message = message;
 this.MessageTitle = title.ToUpper();
 this.DataContext = this;

 public string Message { get; set; }
 public string MessageTitle { get; set; }

 public ContentDialogResult DiagResult { get; set; }

 private void OkBtn_Click(object sender, RoutedEventArgs e)
 DiagResult = ContentDialogResult.Primary;

 private void CancelBtn_Click(object sender, RoutedEventArgs e)
 DiagResult = ContentDialogResult.Secondary;

 private void ContentDialog_PrimaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)

 private void ContentDialog_SecondaryButtonClick(ContentDialog sender, ContentDialogButtonClickEventArgs args)

In the above code:

  1. We have 3 properties to hold the message to be displayed (Message), the title of the message box (MessageTitle) and the result of the dialog (DiagResult)
  2. When the user clicks OK, we set the result of the dialog to ContentDialogResult.Primary
  3. When the user clicks Cancel, we set the result of the dialog to ContentDialogResult.Secondary

Lastly, We need to show the message box that we’ve just created. We’ll do that like this:

MessageDisplay md = new MessageDisplay("This is to test how the message box displays", "Testing");

md.Closing += (s, args) =>
if(md.DiagResult == ContentDialogResult.Primary)
//Do something here if the user clicks the "OK" button

await md.ShowAsync();

The result look like this:


If you feel stuck, you can download my code files here.

In conclusion, you can extend the ContentDialog to suit other purposes such as a login view or other information collection views. If you have any comment or you know a better way to create a custom message box and the use of ContentDialog, kindly drop a comment below. HAPPY CODING!