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

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

我补充说:

using System.Windows; 

但是当我input:

 MessageBox.Show(""); 

我收到错误:

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

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

我想念什么? 或者有另一种方式如何显示像messagebox的东西?

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

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

只是想补充到僵尸Sheep的答案:另外,定制是很容易的

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

触发某些function当单击“是”或“否”时,您还可以使用:

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

你也可以做一个像下一个class。 下面的代码是一个用法示例:

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

只是使用:

 using System.Windows.Forms;