Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop MainWindow from closing whole application

Tags:

c#

window

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.

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?

like image 537
CareTaker22 Avatar asked Mar 18 '16 08:03

CareTaker22


1 Answers

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.

like image 52
Frerk Morrin Avatar answered Nov 15 '22 07:11

Frerk Morrin