在WPF中显示多个对话框是否安全? [英] Is it safe to show multiple dialogs in WPF?

查看:90
本文介绍了在WPF中显示多个对话框是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

令人惊讶的是,通过将ShowDialog()调用放在Dispatcher上,一次可以显示多个对话框:

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有时不知道哪个对话框应该位于顶部-但仍然允许您通过单击将其中一个对话框置于最前面,这对对话框是不常见的,因为通常不允许在对话框外部单击. /p>

更新:我遇到的一个问题是,如果您隐藏了一个对话框,则用户可以再次与所有其他Windows进行交互! (而不仅仅是其他对话框).请参阅: WPF对话框不是模态的吗?

解决方案

显示对话框不会阻止UI线程-否则您将无法与对话框进行交互.

它仅标志着以下事实:存在未完成的模态对话框,并且应禁用对所有其他非对话窗口的输入.

如果将ShowDialog调用拖到调度程序中,则调度程序将允许创建其他对话框,因为您没有执行模态对话框突出时禁止的操作-输入其他非对话框窗口

您的新对话框具有完整的功能,因为它是一个对话框,并且您不打算输入非对话窗口.

切换应用程序应将任何模式对话框置于最前面,但是由于您有多个模式对话框,因此系统会感到困惑,即哪个模式对话框应该位于最顶层.我建议您捕获激活事件,然后手动将必要的对话框置于最顶部.

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?

解决方案

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.

这篇关于在WPF中显示多个对话框是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆