【发布时间】:2009-12-01 15:05:00
【问题描述】:
我正在编写一个应用程序 (c# + wpf),其中所有模式样式对话框都实现为覆盖主要 Window 的半透明网格顶部的 UserControl。这意味着只有一个Window,它保持了所有公司应用程序的外观。
要显示MessageBox,语法如下:
CustomMessageBox b = new CustomMessageBox("hello world");
c.DialogClosed += ()=>
{
// the rest of the function
}
// this raises an event listened for by the main window view model,
// displaying the message box and greying out the rest of the program.
base.ShowMessageBox(b);
如您所见,与经典的 .NET 版本相比,不仅执行流程实际上是倒置的,而且还非常冗长:
MessageBox.Show("hello world");
// the rest of the function
我真正在寻找的是一种在它引发对话框关闭事件之前不从base.ShowMessageBox返回的方法,但我看不出如何在不挂起GUI线程的情况下等待这个,从而防止用户曾经单击确定。我知道我可以将委托函数作为 ShowMessageBox 函数的参数,这可以防止执行反转,但仍然会导致一些疯狂的语法/缩进。
我是否遗漏了一些明显的东西,或者是否有标准的方法来做到这一点?
【问题讨论】:
标签: c# wpf events dialog messagebox