简体   繁体   English

如何阻止MainWindow关闭整个应用程序

[英]How to stop MainWindow from closing whole application

I'm trying to close my Main Window from a child window in my WPF application. 我正在尝试从我的WPF应用程序中的子窗口关闭我的主窗口。 The problem is, once I try to 'close' the main window, my whole application closes. 问题是,一旦我试图“关闭”主窗口,我的整个应用程序就会关闭。

Here is my coding from my main window(pgLogin): 这是我的主窗口(pgLogin)的编码:

Window nextWindow = null;
nextWindow = new pgDashboard();
nextWindow.Owner = this;
this.Hide();
nextWindow.Show();

And my child window(pgDashboard): 和我的孩子窗口(pgDashboard):

public static T IsWindowOpen<T>(string name = null) where T : Window
{
    var windows = Application.Current.Windows.OfType<T>();
    return string.IsNullOrEmpty(name) ? windows.FirstOrDefault() : windows.FirstOrDefault(w => w.Name.Equals(name));
}


private void HAZEDashboard_Loaded(object sender, RoutedEventArgs e)
{
    var credentials = this.Owner as pgLogin;
    credentials.txtEmailAddress.Text.ToString();

    var window = IsWindowOpen<pgLogin>();

    if (window != null)
    {
        window.Close();
    }
}

Is there a way to close the main window without hiding it and still keeping open my child window? 有没有办法关闭主窗口而不隐藏它仍然保持打开我的孩子窗口?

Goto to the Applications App.xaml and Change the "ShutdownMode", for example to "OnExplicitShutdown". 转到应用程序App.xaml并将“ShutdownMode”更改为“OnExplicitShutdown”。 The default is ShutdownMode="OnMainWindowClose" which results in the behaviour you described. 默认值为ShutdownMode =“OnMainWindowClose”,这会导致您描述的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM