隐藏窗口隐藏了MainWindow

时间:2016-08-08 20:14:11

标签: c# wpf xaml

我的应用程序中除了MainWindow之外还有另一个窗口,我覆盖了这样的关闭方法,因为我不希望窗口完全关闭:

private void Window_Closing(object sender, CancelEventArgs e)
{
    e.Cancel = true;
    this.Visibility = Visibility.Hidden;
}

然后在构造函数中:

public Inputwindow()
{
    InitializeComponent();
    this.Closing += Window_Closing;
}

但是现在如果我要关闭我的MainWindow,它只会隐藏MainWindow。

无法弄清楚,如何让它发挥作用。

1 个答案:

答案 0 :(得分:4)

每个WPF应用程序都有一个名为Application.ShutdownMode的属性,该属性确定应用程序何时实际结束。默认情况下,它设置为OnLastWindowClose,这意味着在所有窗口关闭之前应用程序不会结束(即使主窗口关闭)。听起来你希望你的应用程序在主窗口关闭时结束,即使其他窗口没有关闭,只是隐藏,这将对应于OnMainWindowClose模式。以下是相关的MSDN文档:demo

要在xaml中设置属性,请打开App.xaml文件并添加ShutdownMode属性,如下所示:

<Application x:Class="TestClass"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ShutdownMode="OnMainWindowClose">       

</Application>