Joakim Carlsson
Joakim Carlsson

Reputation: 1590

WPF MainWindow doesn't close with .Close

I'm working on a small project where I have a login window and when the user is autheticated it will open a new window, that works but after the user logged in I want to close the Login window how ever, the main window doesn't close.

MainWindow mainWindow = new MainWindow();
mainWindow.Close();
Loader loader = new Loader();
loader.Show();

Upvotes: 0

Views: 110

Answers (1)

Ame no Kenshi
Ame no Kenshi

Reputation: 42

You are initializing a new instance of a Window and close it immediateley afterwards. It looks like your mainWindowis not the login window you want to close. If you want to close the current Main Window of you application, you can use:

App.Current.MainWindow.Close();

Make sure you also set a new Main Window. I assume you wanted to do something similar to this, also assuming "loader" is your new Window:

private void Login(){
    App.Current.MainWindow.Close();
    Loader loader = new Loader();
    App.Current.MainWindow = loader;
}

Upvotes: 1

Related Questions