David
David

Reputation: 2563

WPF: Close and open windows

In my case I have a main window and a login view.

When the main window is closed, the login window should be displayed. I close it like so:

    void CloseOnCompleteAndDisplayLogin(object sender, RunWorkerCompletedEventArgs e)
    {
        this.Close();
        new Login().Show();
    }

In the login window I open the MainWindow like so:

this.Hide();
var window = new MainWindow(model).Show();

Problem: When I login and open the MainWindow the first time it works fine. When I close the MainWindow and login again, several functions of the main window stop working or start throwing exceptions.

What am I doing wrong here? I already figured that Window_Loaded like events are not triggered on the re-login.

Upvotes: 2

Views: 4895

Answers (2)

Stecya
Stecya

Reputation: 23266

Yes, Loaded is not triggered if you Hide/Show. Instead use the Shown event to initialize your logic.

Upvotes: 3

Dariusz Wickowski
Dariusz Wickowski

Reputation: 191

I think that this could be the problem

       new Login().Show();

first you hide your instance of login window and after closing MainWindow you always create new()

Upvotes: 0

Related Questions