【问题标题】:Universal Apps MessageBox: "The name 'MessageBox' does not exist in the current context"Universal Apps MessageBox:“当前上下文中不存在名称‘MessageBox’”
【发布时间】:2014-04-07 10:06:25
【问题描述】:

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

我补充说:

using System.Windows;

但是当我输入时:

MessageBox.Show("");

我得到错误:

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

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

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

【问题讨论】:

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


    【解决方案1】:

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

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

    【讨论】:

    • 整个异步业务就像病毒一样。你开始把它放在一个地方,它会传播到所有地方,让你重构大量的代码。不确定这是有意的还是我做错了什么
    • @Rajiv afaik 这是有意的,但如果您发现任何反对/支持此内容的内容,请告诉我。
    • @Rajiv:如果您使调用方法与 void(或除任务/任务 之外的任何其他返回类型)异步,它不会在您的代码中传播。包含方法声明中的 async 关键字本身不需要任何重构!
    【解决方案2】:

    只是想补充一下 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)
            { *** }
    

    【讨论】:

      【解决方案3】:

      试试这个:

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

      【讨论】:

        【解决方案4】:

        你也可以制作一个类似下一个的课程。代码下方是一个使用示例:

        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 的线程阻塞行为不同,可能会让新开发人员感到非常困惑。
        【解决方案5】:
        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();
            }
        }
        

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2013-10-02
            • 2014-04-22
            • 2017-06-17
            • 2015-09-11
            • 1970-01-01
            • 1970-01-01
            • 2022-12-18
            相关资源
            最近更新 更多