Windows Store (WinRT) – How to display a message box in a Windows Store application.


If you program WinForms, you know MsgBox/MessageBox.Show. Buried in the new WinRT namespaces there is a popup that will allow you to have a simple popup:

VB.Net

Dim dialog As New Windows.UI.Popups.MessageDialog(msg)
Await dialog.ShowAsync()

To use this, the function/sub that you call it from will need to be marked “Async”. Example:

VB.Net

Private Async Sub ButtonMultiple_Click(sender As Object, e As RoutedEventArgs) Handles ButtonMultiple.Click
    ' Show an error if it occurs.
    Dim errorMsg As String = ""
    Try
        TextBlockCalc.Text = _operationStack.Calculate.ToVerticalString
    Catch ex As Exception
        errorMsg = ex.Message
    End Try
    If String.IsNullOrWhiteSpace(errorMsg) = False Then
        Await MsgBox(errorMsg)
    End If
End Sub

The one catch is, you can’t call this from inside a Catch/Finally method. In this instance here, I set a string value with the message from the exception and then display it later (this is just for example).