简体   繁体   中英

How to close Window in WinUI3?

I have an app that consists of 2 parts. 1st part is Login form, where user needs to enter login and password. If they are correct, it start "Editor" window where user can work.

For now in order to launch second window I use:

var editorWindow = new EditorWindow();
editorWindow.Activate();

The problem is that Login window is still there, and while it is not critical, I still want to close it after Login is done.

First time I tried to add Window.Close() after opening the 2nd window in the.cs file of 1st Window, so

var editorWindow= new EditorWindow();
editorWindow.Activate();

var oldWindow = new MainWindow();
oldWindow.Close();

Which resulted Attempted to read or write protected memory eror. I tried to do it in the 2nd Window.cs file like this:

this.InitializeComponent();

var oldWindow = new MainWindow();
oldWindow.Close();

Which resulted the same error

So how can I do this properly?

If you open the second window in the code-behind of the first window, you should be able to just call this.Close() right after you've called Activate() on the new window:

var editorWindow= new EditorWindow();
editorWindow.Activate();

this.Close();

If you open the EditorWindow from somewhere else, you need to get a reference to the first window to be able to close it. You could for example use a variable in the App class for this as suggested here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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