C# 显示隐藏的 WPF 窗口
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17994453/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Showing a hidden WPF window
提问by Mahmoud Samy
In a WPF window I want to hide it, show another window using ShowDialog
then unhide the first window.
在 WPF 窗口中,我想隐藏它,使用ShowDialog
然后取消隐藏第一个窗口显示另一个窗口。
When I do that:
当我这样做时:
this.Hide();
var window2 = new Window2();
window2.ShowDialog();
this.Show();
The first window open as a blank and empty window.
第一个窗口作为空白窗口打开。
What's wrong in this technique?
这种技术有什么问题?
When I do that:
当我这样做时:
var window2 = new Window2();
Visibility = Visibility.Collapsed;
window2.ShowDialog();
Visibility = Visibility.Visible;
The first window exits then the application.
第一个窗口退出,然后应用程序。
What's wrong in this technique too?
这种技术也有什么问题?
采纳答案by Anthony Russell
You shouldn't be using hide in WPF. That is WinForms shenanigans.
您不应该在 WPF 中使用 hide。这就是 WinForms 的恶作剧。
Do this instead:
改为这样做:
this.Visibility = Visibility.Collapsed;
...
this.Visibility = Visibility.Visible;
Also, I saw your comment above that this doesn't work. However, I started a new WPF project, did this, built and ran it. It works.
另外,我在上面看到您的评论,这不起作用。然而,我开始了一个新的 WPF 项目,做了这个,构建并运行了它。有用。
Note that there are no errors.
请注意,没有错误。
回答by yo chauhan
Window2 window2 = new Window2();
this.Visibility = Visibility.Collapsed;
window2.ShowDialog();
this.Visibility = Visibility.Visible;
回答by Artiom
In my case next code worked out:
在我的情况下,下一个代码解决了:
new HiddenWindow
{
WindowStyle = WindowStyle.None,
AllowsTransparency = true,
Opacity = 0.0
}.Show();
回答by Randy Beers
foreach (Window window in App.Current.Windows)
{
if (!window.IsActive)
{
window.Show();
}
}
Works fine for me
对我来说很好用
回答by uTILLIty
Check your Application.Current.ShutdownMode
. Closing the window you assign to Application.Current.MainWindow
will cause the application to shutdown, if Application.Current.ShutdownMode
is set to OnMainWindowClose
. In your case I would set it to OnExplicitShutdown
. You can see the default values being set in app.xaml
of your WPF application project.
检查您的Application.Current.ShutdownMode
. Application.Current.MainWindow
如果Application.Current.ShutdownMode
设置为,则关闭您分配给的窗口将导致应用程序关闭OnMainWindowClose
。在您的情况下,我会将其设置为OnExplicitShutdown
. 您可以看到在app.xaml
WPF 应用程序项目中设置的默认值。