【问题标题】:MessageBox Features in WPFWPF 中的 MessageBox 功能
【发布时间】:2015-09-14 20:34:52
【问题描述】:

我正在尝试创建一个出现在程序开头的 MessageBox,询问用户是否要加载文件。到目前为止,我有:

public static void LoadFile(object sender, FormClosingEventArgs e)
{
    System.Windows.MessageBox.Show("Would you like to load a file?",
    System.Windows.MessageBoxButton.YesNo, System.Windows.MessageBoxQuestion);

    if (result == DialogResult.No)
    {
        // cancel the closure of the form.
        e.Cancel = true;
    }
}

我意识到其中一些代码用于退出程序。我不打算这样做,它只是目前仍留在我正在尝试的示例代码中。当我尝试这段代码时,我收到了几个错误,主要的一个涉及MessageBoxQuestion。错误读取

命名空间 System.Windows 中不存在类型或命名空间名称“MessageBoxQuestion”

我之前在 MessageBoxButtons 上遇到过此错误,但通过将其更改为 MessageBoxButton 来修复它。从一个简单的消息框开始,我最初有代码:

public static void LoadFile()
{
    System.Windows.MessageBox.Show("Text");
}

尽管我必须添加 System.Windows. 来消除错误,但效果非常好

名称 MessageBox 在当前上下文中不存在。

有人知道如何让我的MessageBox 正常工作吗?

【问题讨论】:

    标签: c# wpf winforms messagebox


    【解决方案1】:

    MessageBox 的 WPF 版本与 Windows 窗体的版本不同。你需要使用this重载。

    【讨论】:

      【解决方案2】:

      这是我最终想到的:

      public static void LoadFile()
          {
              // Configure message box 
              string message = "Would you like to load a file?";
              string caption = "Startup";
              System.Windows.MessageBoxButton buttons = System.Windows.MessageBoxButton.YesNo;
              System.Windows.MessageBoxImage icon = System.Windows.MessageBoxImage.Information;
              // Show message box
              System.Windows.MessageBoxResult result = 
                 System.Windows.MessageBox.Show(message, caption, buttons, icon);
              if(result == System.Windows.MessageBoxResult.Yes)
              {
      
              }
              else if(result == System.Windows.MessageBoxResult.No)
              {
      
              }
         }
      

      我包含了我计划稍后使用的 if else 分支。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-08-12
        • 1970-01-01
        • 2018-05-03
        • 2021-06-15
        • 1970-01-01
        • 2019-06-22
        • 1970-01-01
        相关资源
        最近更新 更多