通用应用程序消息框:“名称'消息框'在当前上下文中不存在”

本文关键字:消息 上下文 不存在 名称 应用程序 | 更新日期: 2023-09-27 17:57:20

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

我补充说:

using System.Windows;

但是当我输入时:

MessageBox.Show("");

我收到错误:

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

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

我错过了什么吗?或者有没有另一种方法可以显示消息框之类的内容?

通用应用程序消息框:“名称'消息框'在当前上下文中不存在”

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

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

只是想补充僵尸羊的答案: 另外,定制非常简单

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

试试这个:

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

你也可以创建一个类似的类。代码下面是一个用法示例:

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

对于新的 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 的各种用法。