无法使用 DialogResult

发布于 2024-11-14 02:55:44 字数 425 浏览 6 评论 0原文

我尝试使用 DialogResult 来检查 MessageboxYesNoCancel。我正在使用以下代码,我没有看到任何问题:

DialogResult dlgResult = MessageBox.Show(
   "Save changes before closing?", 
   "Warning", 
   MessageBoxButton.YesNoCancel, 
   MessageBoxImage.Question);

但是 Visual Studio 向我抛出错误说

'System.Windows.Window.DialogResult' 是一个“属性”,但使用起来就像 '类型'

I tried to use DialogResult to check an Messagebox's YesNoCancel. I'm using the following code which I don't see any problem with:

DialogResult dlgResult = MessageBox.Show(
   "Save changes before closing?", 
   "Warning", 
   MessageBoxButton.YesNoCancel, 
   MessageBoxImage.Question);

But Visual Studio throws me error saying

'System.Windows.Window.DialogResult'
is a 'property' but is used like a
'type'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

清音悠歌 2024-11-21 02:55:44

DialogResult 枚举窗口.DialogResult 属性

要解决此问题,可以使用枚举的完全限定名称。如下所示:

System.Windows.Forms.DialogResult dlgResult = ...

但是,由于您使用的是WPF,因此请使用MessageBoxResult Enumeration 来获取消息的结果:

MessageBoxResult result = 
    MessageBox.Show("Would you like to see the simple version?", 
    "MessageBox Example", MessageBoxButton.OKCancel);

There is a confliction here between the DialogResult Enumeration and the Window.DialogResult Property.

To solve this problem, you can use the fully qualified name of the enumuration. As the following:

System.Windows.Forms.DialogResult dlgResult = ...

However, since you are using WPF, use MessageBoxResult Enumeration to get the result of the message:

MessageBoxResult result = 
    MessageBox.Show("Would you like to see the simple version?", 
    "MessageBox Example", MessageBoxButton.OKCancel);
苏辞 2024-11-21 02:55:44

问题是 DialogResult 也是表单的一个属性,编译器认为您正在引用此属性。

这里有多种选择:

  • 使用类型的完全限定名称 System.Windows.Forms.DialogResult
  • 使用 var 让编译器找出类型并摆脱名称冲突

The problem is DialogResult is also a property of the form and the compiler thinks you are refering to this property.

You have several choices here:

  • Use the fully qualified name of the type System.Windows.Forms.DialogResult
  • Use var to let the compiler figure out the type and get rid of the name collision
﹉夏雨初晴づ 2024-11-21 02:55:44

DialogResult 不是一种类型,它是一个属性,您想要类型 MessageBoxResult

我可以从您没有使用 winforms 的问题中看出。所以代码会这样读:

MessageBoxResult result = MessageBox.Show(
    "Save changes before closing?",
    "Warning",     
    MessageBoxButton.YesNoCancel,
    MessageBoxImage.Question);

DialogResult is not a type, its a property, you want the type MessageBoxResult

I can see from the question you are not using winforms. So the code would read,

MessageBoxResult result = MessageBox.Show(
    "Save changes before closing?",
    "Warning",     
    MessageBoxButton.YesNoCancel,
    MessageBoxImage.Question);
情泪▽动烟 2024-11-21 02:55:44

尝试将 dlgResult 声明为 var。然后它应该可以工作

    var dlgResult = 
        MessageBox.Show("Save changes before closing?", 
            "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

WPF下的MessageBox.Show确实返回MessageBoxResult 而不是 DialogResultDialogResult 用于WindowsForms

Try to declare dlgResult as var. Then it should work

    var dlgResult = 
        MessageBox.Show("Save changes before closing?", 
            "Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);

Also MessageBox.Show under WPF does return MessageBoxResult and not DialogResult. DialogResult is used in WindowsForms.

冷了相思 2024-11-21 02:55:44

只需尝试使用 MessageBoxResult

MessageBox 将返回 MessageBoxResult 枚举值

            MessageBoxResult dlgResult = MessageBox.Show("Save changes before closing?","Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
        Console.WriteLine(dlgResult);

just try with MessageBoxResult

MessageBox will return MessageBoxResult enum values

            MessageBoxResult dlgResult = MessageBox.Show("Save changes before closing?","Warning", MessageBoxButton.YesNoCancel, MessageBoxImage.Question);
        Console.WriteLine(dlgResult);
灯下孤影 2024-11-21 02:55:44
MessageBoxResult result = MessageBox.Show(
"Save changes before closing?",
"Warning",     
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);

然后使用结果来检查

MessageBoxResult result = MessageBox.Show(
"Save changes before closing?",
"Warning",     
MessageBoxButton.YesNoCancel,
MessageBoxImage.Question);

then use result to check

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文