0
votes

When closing my main window(pgLogin) from a child window(pgDashboard), my child window does not want to display at all. In my previous question I set the "ShutdownMode" to "OnExplicitShutdown", so that when I close my main window, the whole application does not shut down. Only thing now is that my application does not shut down, but my child window does not display at all.

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();
    }
}

Any idea why this could be happening?

EDIT: Just did a test, and I can see that when I close the main window, my child window also closes for some reason, because when I try to call this.Show(); on my child window, it gives me this error:

Cannot set Visibility or call Show, ShowDialog, or WindowInteropHelper.EnsureHandle after a Window has closed.

EDIT 2: I think the problem might be caused because I set the main window(pgLogin) as the owner of the child window(pgDashboard)?

1
regarding your previous question: stackoverflow.com/questions/5708992/how-to-switch-wpf-windows; i think this would be a better way to solve your issue - ASh
@ASh Thank you for steering me in the right direction! I figured it out. :) - CareTaker22

1 Answers

0
votes

I figured it out. This is what I did:

My main window(pgLogin):

Window nextWindow = null;
nextWindow = new pgDashboard();
App.Current.MainWindow = nextWindow;
nextWindow.Show();

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 window = IsWindowOpen<pgLogin>();

    if (window != null)
    {
        var credentials =  window.txtEmailAddress.Text.ToString();
        window.Close();
    }
}

I did not set the owner of the child window(pgDashboard) to pgLogin, because when I close the main window (pgLogin), all of the windows that the main window owns, closes as well.