简体   繁体   English

WPF中的MessageBox功能

[英]MessageBox Features in WPF

I am trying to create a MessageBox that appears at the start of my program, asking the user if they would like to load a file or not. 我试图创建一个出现在程序开始时的MessageBox,询问用户是否要加载文件。 So far I have: 到目前为止,我有:

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

I realize that some of this code is used to exit out of the program. 我意识到其中一些代码用于退出程序。 I don't intend to do this, it is just currently still left in from the sample code I was trying. 我不打算这样做,它只是目前仍在我尝试的示例代码中。 When I try this code, I receive several errors, the major one involves the MessageBoxQuestion . 当我尝试这段代码时,我收到几个错误,主要错误涉及MessageBoxQuestion The error reads 错误读取

The type or namespace name 'MessageBoxQuestion' does not exist in the namespace System.Windows 类型或名称空间名称'MessageBoxQuestion'在名称空间System.Windows中不存在

I previously had this error on the MessageBoxButtons but fixed it by changing it to MessageBoxButton . 我以前在MessageBoxButtons上遇到此错误,但通过将其更改为MessageBoxButton来解决了该错误。 Starting with just a simple message box, I originally had the code: 从一个简单的消息框开始,我最初有以下代码:

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

This worked perfectly fine, despite the fact that I had to add the System.Windows. 尽管事实上我必须添加System.Windows. ,但这仍然可以正常工作System.Windows. to remove the error 消除错误

The name MessageBox does not exist in the current context. 名称MessageBox在当前上下文中不存在。

Does anyone know a solution as to how I can get my MessageBox working correctly? 有谁知道如何使我的MessageBox正常工作的解决方案?

The WPF version of MessageBox differs from the Windows Forms' version. WPF的MessageBox版本与Windows窗体的版本不同。 You need to use this overload. 您需要使用重载。

Here is what I finally came up with: 这是我最终想出的:

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

        }
   }

I included the if else branch that I planned to use later. 我包括了以后计划使用的if else分支。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM