62

I want to use MessageBox for showing download errors in my WP8.1 app.

I added:

using System.Windows;

but when I type:

MessageBox.Show("");

I get error:

"The name 'MessageBox' does not exist in the current context"

In Object Browser I found that such class should exist and in "Project->Add reference... ->Assemblies->Framework" is shown that all assemblies are referenced.

Do I miss something? Or is there another way how to show something like messagebox?

Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
sprrw
  • 623
  • 1
  • 5
  • 4

6 Answers6

127

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();
Claus Jørgensen
  • 25,882
  • 9
  • 87
  • 150
ZombieSheep
  • 29,603
  • 12
  • 67
  • 114
  • 4
    This whole Async business is like a virus. You start putting it in one place and it spreads to everything, making you refactor a large amount of code. Not sure if this is intended or I am doing something wrong – Rajiv Aug 06 '15 at 02:02
  • @Rajiv afaik it is intended, but let me know if you find anything that is against/supports this, let me know. – ave Aug 06 '15 at 13:32
  • @Rajiv: it does not spread throughtout your code if you make your calling method async with void (or any other return type than Task/Task<>). The async keyword in the containing method declaration per se does not call for any refactorings! – henon Nov 24 '16 at 12:29
48

Just wanted to add to ZombieSheep's answer: also, customization is quite simple

        var dialog = new MessageDialog("Are you sure?");
        dialog.Title = "Really?";
        dialog.Commands.Add(new UICommand { Label = "Ok", Id = 0 });
        dialog.Commands.Add(new UICommand { Label = "Cancel", Id = 1 });
        var res = await dialog.ShowAsync();

        if ((int)res.Id == 0)
        { *** }
Vitalii Vasylenko
  • 4,776
  • 5
  • 40
  • 64
30

try this:

 using Windows.UI.Popups;

code:

private async void Button_Click(object sender, RoutedEventArgs e)
    {

        MessageDialog msgbox = new MessageDialog("Would you like to greet the world with a \"Hello, world\"?", "My App");

        msgbox.Commands.Clear();
        msgbox.Commands.Add(new UICommand { Label = "Yes", Id = 0 });
        msgbox.Commands.Add(new UICommand { Label = "No", Id = 1});
        msgbox.Commands.Add(new UICommand { Label = "Cancel", Id = 2 });

        var res = await msgbox.ShowAsync(); 

        if ((int)res.Id == 0)
        {
            MessageDialog msgbox2 = new MessageDialog("Hello to you too! :)", "User Response");
            await msgbox2.ShowAsync();
        }

        if ((int)res.Id == 1)
        {
            MessageDialog msgbox2 = new MessageDialog("Oh well, too bad! :(", "User Response");
            await msgbox2.ShowAsync();
        }

        if ((int)res.Id == 2)
        {
            MessageDialog msgbox2 = new MessageDialog("Nevermind then... :|", "User Response");
            await msgbox2.ShowAsync();
        }


    }

To Trigger some Function When "Yes" or "No" is clicked, You can also use:

msgbox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.TriggerThisFunctionForYes)));
msgbox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.TriggerThisFunctionForNo)));
AKM
  • 509
  • 9
  • 10
2

You could also make a class like the next one. Below the code a usage example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI.Popups;

namespace someApp.ViewModels
{
    public static class Msgbox
    {
        static public async void Show(string mytext)
        {
            var dialog = new MessageDialog(mytext, "Testmessage");
            await dialog.ShowAsync();
        }
    }

}

The example to use it in a class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace someApp.ViewModels
{
    public class MyClass{

        public void SomeMethod(){
            Msgbox.Show("Test");
        }

    } 
}
Snamelisch
  • 77
  • 5
  • This implementation doesn't mimic the same thread blocking behavior as the original MessageBox and could be very confusing for new developers. – Alex Marshall Feb 22 '18 at 00:30
2
public sealed partial class MainPage : Page {
    public MainPage() {
        this.InitializeComponent();
    }

    public static class Msgbox {
        static public async void Show(string m) {
            var dialog = new MessageDialog( m);            
            await dialog.ShowAsync();
        }
    }

    private void Button_Click(object sender, RoutedEventArgs e) { 
        Msgbox.Show("This is a test to see if the message box work");
        //Content.ToString();
    }
}
pushkin
  • 9,575
  • 15
  • 51
  • 95
Mohammed
  • 21
  • 1
0

For new UWP apps (starting from Windows 10) Microsoft recommends using ContentDialog instead.

Example:

private async void MySomeMethod()
{
    ContentDialog dlg = new ContentDialog()
    {
        Title = "My Content Dialog:",
        Content = "Operation completed!",
        CloseButtonText = "Ok"
    };

    await dlg.ShowAsync();
}

Usage:

private void MyButton_Click(object sender, RoutedEventArgs e)
{
   MySomeMethod();
}

Remark: You can use different styles etc. for the ContentDialog. Please refer to the link above for various usages for ContentDialog.

nam
  • 21,967
  • 37
  • 158
  • 332