[英]How do I “Hide()” a Modal WPF Window without it closing?
I have a WPF window that is run on a background thread as a sort of "notifier window"... when an event is raised, it displays a message... a user clicks the "Snooze" button and I call this.Visibility = Visibility.Collapsed
我有一个WPF窗口,它作为一种“通知程序窗口”在后台线程上运行...当一个事件被引发时,它会显示一条消息......用户点击“贪睡”按钮然后我调用它。
this.Visibility = Visibility.Collapsed
The very moment that I hide the window (either by calling this.Hide()
or setting the Visibility
as mentioned above)... the "ShowDialog()" code releases the window and closes it. 我隐藏窗口的那一刻(通过调用
this.Hide()
或设置上面提到的Visibility
)......“ShowDialog()”代码释放窗口并关闭它。
This is absolutely a bug in the WPF code (which I've identified via reflector)... but my question remains. 这绝对是WPF代码中的一个错误(我通过反射器识别出来的......)但我的问题仍然存在。 Has anyone been able to come up with a work-around for this issue?
有没有人能够为这个问题找到解决办法?
I've tried many things and am now reaching out to ya'll smart people :) 我已经尝试了很多东西,现在我正在接触到聪明的人:)
You can't hide a modal dialog. 您无法隐藏模态对话框。 That's like asking, "How do I get to 100mph in reverse?"
这就像是在问:“我如何反向达到100英里/小时?” You don't, you drive the car forwards.
你没有,你开车前进。
Use Show, not ShowDialog. 使用Show,而不是ShowDialog。 Alternately you can simply re-ShowDialog when it needs to become visible again.
或者,当需要再次显示时,您可以简单地重新显示ShowDialog。
In order to show modal window always use ShowDialog()
. 为了显示模态窗口,总是使用
ShowDialog()
。
Use Close()
instead of Hide()
. 使用
Close()
而不是Hide()
。
Handle the FormClosing
event like that: 像这样处理
FormClosing
事件:
private void OnFormClosing(object sender, FormClosingEventArgs e) { e.Cancel = true; this.Visible = false; }
Timothy's Answer is good. 提摩太的答案很好。 I just needed for my scenerio to add the following
我只需要为我的scenerio添加以下内容
window.Closed += new EventHandler(window_Closed);
window.Show();
System.Windows.Threading.Dispatcher.Run();
and then in the event... 然后在事件中......
void window_Closed(object sender, EventArgs e)
{
System.Windows.Threading.Dispatcher.ExitAllFrames();
}
I needed to do this because it was hanging on the Run after the form was really closed. 我需要这样做,因为它在表单真正关闭后挂在Run上。
OK, and as quickly as that - my boss (old C++ goofy guy that he is) figured out the answer. 好吧,并且尽快 - 我的老板(他是老C ++傻傻的家伙)想出了答案。
Here was the code inside of my background thread (which is set to STA mode): 这是我的后台线程中的代码(设置为STA模式):
// Show dialog - keeps the thread open and shows the window! Yay!!!
new BeamUI.Notifier.NotifierWindow().ShowDialog();
And here is the modification, that strangely enough works perfectly :) 这是修改,奇怪的是足够完美:)
// Show... hmm, that shows the window... but how do I keep this thread open?
new BeamUI.Notifier.NotifierWindow().Show();
// ZOMG - a line of code that JUST keeps the thread (and msgpump) going!!!
System.Windows.Threading.Dispatcher.Run();
And that's it. 就是这样。
This kinda thing makes me hate C++ people though, and makes me want to just say "if you just built it right in the first place I wouldn't have to look for a work-around!" 这种事情让我讨厌C ++人,并且让我想说“如果你刚开始构建它,我就不用去寻找解决方法!” (j/k)
(J / K)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.