Search code examples
vb.netwindows-phone-8messagebox

WPF8 Messagebox


I'm trying to implement this code:

 Dim result As MessageBoxResult = _
 MessageBox.Show("Would you like to see the simple version?", _
 "MessageBox Example", MessageBoxButton.OKCancel)

 If (result = MessageBoxResult.OK) Then
     MessageBox.Show("No caption, one button.")
 End If

But getting an error: Type 'MessageBoxResult' is not defined Why is happening?

I'm using: Microsoft Visual Studio Professional 2013 Version 12.0.30501.00 Update 2 Microsoft .NET Framework Version 4.5.51641


Solution

  • If System.Windows didn't resolve then you had a Windows Phone Runtime app (targetting Windows Phone 8.1) rather than a Windows Phone Silverlight app (for either 8.0 or 8.1). Chubosaurus' steps will create a Silverlight app.

    You can confirm in the Solution Explorer, which will show the target for the project. To use System.Windows and MessageBox you will need a Windows Phone Silverlight app.

    Windows Phone Silverlight 8 Windows Phone Silverlight 8.1

    If you have a Windows Phone 8.1 app you can use Windows.UI.Popups.MessageDialog instead enter image description here

    Async Function ShowMyDialog() As Task(Of Boolean)
        Dim result As Boolean = False
        Dim dialog As New Windows.UI.Popups.MessageDialog("Would you like to see the simple version?", "MessageDialog Example")
        dialog.Commands.Add(New Windows.UI.Popups.UICommand("Ok",
            Async Sub(command)
                result = True
                Dim okDialog As New Windows.UI.Popups.MessageDialog("No caption, one button.")
                Await okDialog.ShowAsync()
            End Sub))
        dialog.Commands.Add(New Windows.UI.Popups.UICommand("Cancel"))
        dialog.DefaultCommandIndex = 0
        dialog.CancelCommandIndex = 1
        Await dialog.ShowAsync()
        Return result
    End Function