Universal Apps MessageBox:“当前上下文中不存在名称'MessageBox'”

时间:2014-04-07 10:06:25

标签: c# windows-phone-8.1 win-universal-app

我想使用MessageBox在我的WP8.1应用程序中显示下载错误。

我补充说:

using System.Windows;

但是当我输入时:

MessageBox.Show("");

我收到错误:

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

在对象浏览器中,我发现这样的类应该存在,并且在“Project-> Add reference ... - > Assemblies-> Framework”中显示所有程序集都被引用。

我错过了什么吗?或者还有另一种方法来展示像messagebox这样的东西吗?

6 个答案:

答案 0 :(得分:119)

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

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

答案 1 :(得分: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)
        { *** }

答案 2 :(得分: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)));

答案 3 :(得分: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");
        }

    } 
}

答案 4 :(得分:1)

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

答案 5 :(得分:0)

对于新的UWP应用程序(从Windows 10开始),Microsoft建议改为使用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的各种用法。