如何从关闭整个应用程序停止主窗口 [英] How to stop MainWindow from closing whole application

查看:409
本文介绍了如何从关闭整个应用程序停止主窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一个子窗口关闭我的主窗口在我的WPF应用程序。但问题是,一旦我尝试关闭主窗口中,我的整个应用程序关闭

I'm trying to close my Main Window from a child window in my WPF application. The problem is, once I try to 'close' the main window, my whole application closes.

下面是从我的主窗口(pgLogin)我的编码:

Here is my coding from my main window(pgLogin):

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

和我的孩子窗口(pgDashboard):

And my child window(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?

推荐答案

跳转到应用程序的App.xaml和更改ShutdownMode为例为OnExplicitShutdown。
默认为ShutdownMode =OnMainWindowClose这将导致您所描述的行为。

Goto to the Applications App.xaml and Change the "ShutdownMode", for example to "OnExplicitShutdown". The default is ShutdownMode="OnMainWindowClose" which results in the behaviour you described.

这篇关于如何从关闭整个应用程序停止主窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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