Artur Tychina
Artur Tychina

Reputation: 155

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

Upvotes: 0

Views: 729

Answers (2)

Rob Caplan - MSFT
Rob Caplan - MSFT

Reputation: 21889

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

Upvotes: 0

Chubosaurus Software
Chubosaurus Software

Reputation: 8161

Nothing wrong with your code. It should work (I implemented it myself). So it has to be some linking error / install error / or project creation error.

So lets fix, so lets try this:

  • File -> New -> Project
  • Select Template -> Visual Basic -> Store App -> windows Phone App -> Blank App (Windows Phone Silverlight)
  • Select 8.0 as the Target

It should generate you a blank app, then lets try and create a MessageBox on the Page Loaded Event

Imports System
Imports System.Threading
Imports System.Windows.Controls
Imports Microsoft.Phone.Controls
Imports Microsoft.Phone.Shell
Imports System.Windows

Partial Public Class MainPage
    Inherits PhoneApplicationPage

    ' Constructor
    Public Sub New()
        InitializeComponent()

        SupportedOrientations = SupportedPageOrientation.Portrait Or SupportedPageOrientation.Landscape

    End Sub

    Private Sub PhoneApplicationPage_Loaded(sender As Object, e As RoutedEventArgs)
        Dim result As MessageBoxResult = _
        MessageBox.Show("Would you like to see the simple version?", _
        "MessageBox Example", MessageBoxButton.OKCancel)

        If (result = MessageBoxResult.OK) Then
        ' Do whatever
        End If

    End Sub
End Class

If that doesn't work then we need to make sure that System.Windows is imported

  • Right click on the Project -> Properties
  • Click on References
  • Make sure System.Windows has a checkmark

Upvotes: 1

Related Questions