在 WPF 中显示多个对话框安全吗?
令人惊讶的是,通过将 ShowDialog() 调用放在调度程序上,可以一次显示多个对话框:
uiDispatcher.BeginInvoke(new Func<bool?>(myWindow.ShowDialog));
这是如何工作的,并且一旦显示对话框,UI 仍然保持运行以响应用户交互(我本以为不会,因为 ShowDialog( )会阻塞它所在的线程(该线程必须是 UI 线程),甚至可以继续显示新对话框:
Window myWindow;
for(int i = 0; i < 5; i ++)
{
myWindow = new Window();
uiDispatcher.BeginInvoke(new Func<bool?>(myWindow.ShowDialog));
}
并且 UI 仍然具有响应能力。
我应该注意依赖这种行为吗?(当某些后台线程想要时,我想在另一个对话框之上显示一个对话框 - 这有效 - 唯一不需要的行为似乎是在切换应用程序时有时 WPF 不知道哪个对话框应该位于顶部 - 但仍然允许您通过单击其中一个对话框将其置于前面,这对于对话框来说是不常见的,因为通常不允许在对话框外部单击。
更新:我遇到的一个问题是,如果您隐藏其中一个对话框,用户可以再次与所有其他 Windows 进行交互! (不仅仅是其他对话框)。请参阅:WPF 对话框不是模态的?
Surprisingly one can show more than one dialog at a time by putting the ShowDialog() call on the Dispatcher:
uiDispatcher.BeginInvoke(new Func<bool?>(myWindow.ShowDialog));
How come this works and still the UI remains running responding to user interaction once the dialog is shown (I would have thought not since ShowDialog() blocks the thread it is on which has to be the UI thread), one can even go on showing new dialogs:
Window myWindow;
for(int i = 0; i < 5; i ++)
{
myWindow = new Window();
uiDispatcher.BeginInvoke(new Func<bool?>(myWindow.ShowDialog));
}
And the UI is still responsive.
Is there something I should beware of relying on this behaviour? (I want to show one dialog on top of another when some background thread wants to - this works - the only unwanted behaviour seems to be when switching apps sometimes WPF does not know which dialog should be on top - but still allows you to bring one of the dialogs to the front by clicking on it which is unusual for a dialog as clicking outside a dialog is usually not allowed).
UPDATE: One issue I have come across is if you hide one of your dialogs the user can interact with all other Windows again! (not just the other dialogs). See: WPF Dialog not modal?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显示对话框不会阻塞 UI 线程——否则您将无法与对话框交互。
它仅仅标志着存在一个未完成的模式对话框,并且它应该禁用对所有其他非对话框窗口的输入。
如果您将 ShowDialog 调用混入调度程序,则调度程序将允许创建一个附加对话框,因为您没有执行模式对话框未完成时禁止的操作(即输入到其他非对话框窗口)。
您的新对话框功能齐全,因为它是一个对话框,并且您没有尝试在非对话框窗口中输入内容。
切换应用程序应该将任何模态对话框置于最前面,但由于您有多个模态对话框,系统会感到困惑,不知道哪一个应该位于最顶层。我建议您捕获激活事件并手动将必要的对话框置于最顶层。
Showing a dialog does not block the UI thread -- otherwise you won't be able to interact with the dialog.
It merely marks the fact that there is a modal dialog outstanding, and that it should disable inputs to all other non-dialog windows.
If you shuff a ShowDialog call into the dispatcher, the dispatcher will allow an additional dialog to be created because you are not doing something which is prohibited when a modal dialog is outstanding -- which is to input into other non-dialog windows.
Your new dialog is fully functional, because it is a dialog, and you are not trying to input into non-dialog windows.
Switching applications should bring any modal dialog out to the front, but since you have more than one modal dialogs, the system will get confused as to which one should be top-most. I'd suggest you trap the activation event and just manually bring the necessary dialog top-most.