wpf 使用 MessageBox.Show() 阻止所有窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19504249/
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
Block all windows with MessageBox.Show()
提问by DallonF
I'm currently working on a WPF app which has multiple windows. From the "main" window, you should be able to close the entire app. Before the app will be closed, the client wants it to show a dialog box which basically asks "are you sure you want to close the app" and blocks every other window until the user answers.
我目前正在开发具有多个窗口的 WPF 应用程序。从“主”窗口,您应该能够关闭整个应用程序。在应用程序关闭之前,客户端希望它显示一个对话框,该对话框基本上询问“您确定要关闭应用程序吗”并阻止所有其他窗口,直到用户回答。
I'm currently using MessageBox.Show() to create this dialog box, but for some reason it only blocks the main window.
我目前正在使用 MessageBox.Show() 来创建这个对话框,但由于某种原因它只阻塞了主窗口。
Here's the simplest example of what I'm talking about; if you create a WPF window with two buttons:
这是我所说的最简单的例子;如果您创建一个带有两个按钮的 WPF 窗口:
private void openChildWindowButton_Click(object sender, RoutedEventArgs e)
{
var window = new ChildWindow();
window.Show();
}
private void openDialogButton_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show(this, "This should freeze all other windows");
}
Opening a dialog will completely freeze the first window. If you click on it or attempt any sort of interaction, the OS makes a "ding!" sound and flashes the border on the message box. But all of the other windows you've opened can be clicked, moved, resized, etc., and that's what I want to prevent.
打开对话框将完全冻结第一个窗口。如果您单击它或尝试任何类型的交互,操作系统会发出“叮”声!声音并闪烁消息框的边框。但是您打开的所有其他窗口都可以单击、移动、调整大小等,这就是我想要阻止的。
回答by DallonF
As it turns out, there isa way to do this, but it's not pretty. It involves using the WinForms version of MessageBoxand passing an undocumented option as the last property.
事实证明,有是一个办法做到这一点,但它不是漂亮。它涉及使用 WinForms 版本MessageBox并将未记录的选项作为最后一个属性传递。
var result = System.Windows.Forms.MessageBox.Show("Are you sure you want to exit this app?", "Exit", System.Windows.Forms.MessageBoxButtons.YesNo, System.Windows.Forms.MessageBoxIcon.Question, System.Windows.Forms.MessageBoxDefaultButton.Button2, (System.Windows.Forms.MessageBoxOptions)8192 /*MB_TASKMODAL*/);
来源:http: //social.msdn.microsoft.com/Forums/vstudio/en-US/8d1bd4a2-455e-4e3f-8c88-7ed49aeabc09/messagebox-is-not-applicationmodal?forum=wpf
Hopefully this is helpful to somebody else in the future!
希望这对未来的其他人有帮助!
回答by Chris
If this works in WPF like in Windows Forms you could just use:
如果这在 WPF 中像在 Windows 窗体中一样工作,您可以使用:
MessageBox.ShowDialog()
chris
克里斯
Above not working...
以上不工作...
Edit:
编辑:
But there is a workaround: style a Form like a MessageBox (use a fixed Border-Type) and then Show it using ShowDialog(). Then set the Forms Cacel and Ok Button in Properties to your Buttons and you can get a DialogResult just like in a MessageBox. Hope that Helps but it is also from Windows-Forms ;)
但是有一个解决方法:像 MessageBox 一样设置 Form 样式(使用固定的 Border-Type),然后使用 ShowDialog() 显示它。然后将 Properties 中的 Forms Cacel 和 Ok Button 设置为您的按钮,您可以像在 MessageBox 中一样获得 DialogResult。希望有帮助,但它也来自 Windows-Forms ;)

