wpf 不等待 messageBox 的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12060116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:03:42  来源:igfitidea点击:

Doesn't wait for result of messageBox

c#.netwpfmessagebox

提问by Ingó Vals

When catching a unhandled exception in a client application currently in user testing I made a code like this.

在当前正在用户测试中的客户端应用程序中捕获未处理的异常时,我编写了这样的代码。

catch(Exception ex)
{
    var result = MessageBox.Show(ex.Message, "Error", MessageBoxButton.OK);
    Current.Shutdown();
}

But the message box only appears very quickly and then the program shuts down. WHy doesn't the code wait for result to be apparent. How would I do that?

但是消息框只会很快出现,然后程序就会关闭。为什么代码不等待结果明显。我该怎么做?

回答by Renier

I had a similar issue. Problem is that exception is thrown before the main window is displayed and thus there is no Window to display the message box on. Swamy's answer mere forced a time delay so that the window can be initialized. IMO not too elegant. Found a better solution [here]WPF MessageBox not waiting for result [WPF NotifyIcon].

我有一个类似的问题。问题是在显示主窗口之前抛出异常,因此没有窗口可以显示消息框。Swamy 的回答只是强制了一个时间延迟,以便可以初始化窗口。海事组织不太优雅。找到了更好的解决方案 [here] WPF MessageBox not waiting for result [WPF NotifyIcon]

Just specify the MessageBoxOptions.DefaultDesktopOnlyin the MessageBox.Show(....) method.

只需MessageBoxOptions.DefaultDesktopOnlyMessageBox.Show(....) method.

回答by Alexey Ivanov

From the documentation to MessageBoxclass:

从文档到MessageBox类:

Message boxes always have an owner window. By default, the owner of a message box is the window that is currently active in an application at the time that a message box is opened. However, you can specify another owner for the Windowby using one of several Showoverloads. For more information about owner windows, see Window.Owner.

消息框总是有一个所有者窗口。默认情况下,消息框的所有者是打开消息框时应用程序中当前处于活动状态的窗口。但是,您可以Window使用多个Show重载之一为指定另一个所有者。有关所有者窗口的更多信息,请参阅Window.Owner

Thus your problem is really similar to this questionwhere the application exists before message box is dismissed by the user.

因此,你的问题实在是类似这样的问题,其中前消息框被用户驳回申请存在。

The first thing to try is to pass nullas Windowparameter.

首先要尝试的是null作为Window参数传递。

MessageBox.Show(null, ex.Message, "Error", MessageBoxButton.OK);

If you have an instance of Windowclass, for example your main window object, pass it instead of null:

如果您有一个Window类的实例,例如您的主窗口对象,请传递它而不是null

MessageBox.Show(mainWindow, ex.Message, "Error", MessageBoxButton.OK);

If you are currently in any of the main window methods, you can pass thisas the Window:

如果您当前在任何主窗口方法中,则可以this作为 Window传递:

MessageBox.Show(this, ex.Message, "Error", MessageBoxButton.OK);


Use nullonly in case where there's no main window object available at this time.

null仅在此时没有可用的主窗口对象的情况下使用。

Look at MessageBox sample. You should be able to see the difference between the cases where thisis passed as the owner window and where nullis passed.

查看MessageBox 示例。您应该能够看到this作为所有者窗口传递的情况和传递的情况之间的区别null

回答by Gregor Primar

Are you using this code inside the script or actual .net application? You are missing "s" in message box button. you should writte code like this:

您是在脚本中还是在实际的 .net 应用程序中使用此代码?您在消息框按钮中缺少“s”。你应该这样写代码:

try
{
    throw new Exception("My new exception");
}
catch (Exception ex)
{
    DialogResult result = MessageBox.Show(ex.Message, "Error",MessageBoxButtons.OK);
} 

If this will not work you should look at the code that throws your exception for further debugging...

如果这不起作用,您应该查看引发异常的代码以进行进一步调试...

回答by Swamy

This is very old question, but probably below answer may hep some one else,

这是一个非常古老的问题,但可能下面的答案可能会帮助其他人,

I had the same situation where I wanted to show the Message box before the Main window is created, and in My case too , the message Box was disappearing Before I click on OK and application would shut down.

我遇到了同样的情况,我想在创建主窗口之前显示消息框,在我的情况下也是如此,在我单击确定之前,消息框消失了,应用程序将关闭。

I am using async and awaits and adding a delay as low as 10 millisecond fixed the problem for me.(As the main window is loaded by that time)

我正在使用 async 并等待并添加低至 10 毫秒的延迟为我解决了这个问题。(因为那时主窗口已加载)

await Task.Delay(10);
if (MessageBox.Show(MainWindow, "Message", "Error", MessageBoxButton.OK) == MessageBoxResult.OK)
{
    Current.Shutdown();
}

Probably for non Async methods Thread.Sleep could also work (not tried).

可能对于非异步方法 Thread.Sleep 也可以工作(未尝试)。