62

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

我补充说:

using System.Windows;

但是当我输入:

MessageBox.Show("");

我得到错误:

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

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

我错过了什么吗?还是有另一种方式来显示消息框之类的东西?

4

6 回答 6

127

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

var dialog = new MessageDialog("Your message here");
await dialog.ShowAsync();
于 2014-04-07T10:21:14.937 回答
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)
        { *** }
于 2014-09-17T20:09:16.397 回答
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)));
于 2014-09-25T07:35:48.330 回答
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");
        }

    } 
}
于 2017-04-23T10:13:45.873 回答
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();
    }
}
于 2018-05-05T21:14:49.943 回答
1

对于新的 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 的各种用法,请参阅上面的链接。

于 2019-06-19T20:04:12.753 回答