Search code examples
c#wpfwindow

How to stop MainWindow from closing whole application


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.

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

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

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?


Solution

  • 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.