使用MessageBox.Show()阻止所有窗口

时间:2013-10-21 20:40:31

标签: c# wpf windows

我目前正在开发一个有多个窗口的WPF应用程序。在“主”窗口中,您应该能够关闭整个应用程序。在应用程序关闭之前,客户端希望它显示一个对话框,该对话框基本上会询问“您确定要关闭应用程序”并阻止其他所有窗口,直到用户回答。

我目前正在使用MessageBox.Show()创建此对话框,但由于某种原因它只会阻止主窗口。

这是我所说的最简单的例子;如果您使用两个按钮创建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");
} 

打开对话框将完全冻结第一个窗口。如果您单击它或尝试任何类型的交互,操作系统会“叮当作响!”声音并闪烁消息框上的边框。但是你打开的所有其他窗口都可以点击,移动,调整大小等等,这就是我想要阻止的。

2 个答案:

答案 0 :(得分:6)

事实证明, 是一种做到这一点的方法,但它并不漂亮。它涉及使用MessageBox的WinForms版本并将未记录的选项作为最后一个属性传递。

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

希望将来对其他人有帮助!

答案 1 :(得分:0)

如果这在Windows窗体中像WPF一样可以使用:

MessageBox.ShowDialog()

克里斯

以上不工作......

编辑:

但是有一种解决方法:将Form设置为MessageBox(使用固定的Border-Type),然后使用ShowDialog()显示它。然后将Properties中的Forms Cacel和Ok Button设置为您的按钮,您就可以像在MessageBox中一样获得DialogResult。希望能帮助,但它也来自Windows-Forms;)

相关问题