简体   繁体   English

WinUI3中如何关闭Window?

[英]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.如果它们是正确的,它会启动用户可以工作的“编辑器”window。

For now in order to launch second window I use:现在为了启动第二个 window 我使用:

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.问题是Login window还在,虽然不严重,但我还是想在Login完成后关闭它。

First time I tried to add Window.Close() after opening the 2nd window in the.cs file of 1st Window, so我第一次尝试在第一个 Window 的.cs 文件中打开第二个 window 后添加 Window.Close(),所以

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

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

Which resulted Attempted to read or write protected memory eror.这导致Attempted to read or write protected memory I tried to do it in the 2nd Window.cs file like this:我试图在第二个 Window.cs 文件中这样做:

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:如果您在第一个 window 的代码隐藏中打开第二个 window,您应该能够在新的 window 上调用Activate()之后立即调用this.Close()

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.如果从其他地方打开EditorWindow ,则需要获取对第一个 window 的引用才能关闭它。 You could for example use a variable in the App class for this as suggested here .例如,您可以按照此处的建议在App class 中使用变量。

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

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