通用应用程序消息框: "在当前上下文中不存在名称为 'MessageBox' 的内容"

62
我想在我的WP8.1应用中使用MessageBox来显示下载错误。
我添加了:
using System.Windows;

但是当我输入:

MessageBox.Show("");

我遇到了错误:

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

在对象浏览器中,我发现应该存在这样的类,并且在“项目->添加引用...->程序集->框架”中显示所有已引用的程序集。

我错过了什么吗?还是有其他方法可以显示类似消息框之类的东西?

6个回答

127

对于通用应用程序,新的API要求您使用await MessageDialog().ShowAsync()(在Windows.UI.Popups中)使其与Win 8.1保持一致。

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

4
异步编程就像病毒一样。你开始在一个地方使用它,然后它就会传播到其他所有地方,迫使你重构大量的代码。不确定这是有意为之还是我做错了什么。 - Rajiv
@Rajiv 据我所知,这是有意的,但如果你发现任何违反/支持此事的内容,请告诉我。 - ave
@Rajiv:如果您将调用方法异步化为void(或任何返回类型而不是Task / Task <>),则它不会在代码中传播。包含方法声明中的async关键字本身并不需要进行任何重构! - henon

48

只是想补充一下ZombieSheep的回答:此外,自定义非常简单。

        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)
        { *** }

30

试一试:

 using Windows.UI.Popups;

代码:

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();
        }


    }

要在点击“是”或“否”时触发某些功能,您也可以使用以下代码:

msgbox.Commands.Add(new UICommand("Yes", new UICommandInvokedHandler(this.TriggerThisFunctionForYes)));
msgbox.Commands.Add(new UICommand("No", new UICommandInvokedHandler(this.TriggerThisFunctionForNo)));

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();
    }
}

2
您也可以创建一个类,如下所示。以下是使用示例的代码:

您也可以创建下面这样的类。下面是一个使用示例:

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();
        }
    }

}

在类中使用它的示例。
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");
        }

    } 
}

这个实现并不模仿原始MessageBox的线程阻塞行为,可能会让新开发人员感到非常困惑。 - Alex Marshall

0
对于新的 UWP 应用程序(从 Windows 10 开始),微软建议使用 ContentDialog 替代。 示例:
private async void MySomeMethod()
{
    ContentDialog dlg = new ContentDialog()
    {
        Title = "My Content Dialog:",
        Content = "Operation completed!",
        CloseButtonText = "Ok"
    };

    await dlg.ShowAsync();
}

用法:

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

备注:您可以使用不同的样式等来设计ContentDialog。请参考上面的链接了解ContentDialog的各种用法。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接