简体   繁体   English

WPF:关闭和打开窗口

[英]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:在登录窗口中,我像这样打开MainWindow

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

Problem : When I login and open the MainWindow the first time it works fine.问题:当我第一次登录并打开MainWindow时,它工作正常。 When I close the MainWindow and login again, several functions of the main window stop working or start throwing exceptions.当我关闭MainWindow并再次登录时,主窗口的几个功能停止工作或开始抛出异常。

What am I doing wrong here?我在这里做错了什么? I already figured that Window_Loaded like events are not triggered on the re-login.我已经认为Window_Loaded类的事件不会在重新登录时触发。

Yes, Loaded is not triggered if you Hide/Show.是的,如果您隐藏/显示,则不会触发Loaded Instead use the Shown event to initialize your logic.而是使用Shown事件来初始化您的逻辑。

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()首先你隐藏你的登录窗口实例,在关闭 MainWindow 后你总是创建 new()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM