首页 > 解决方案 > 如何在 WPF 中从一个窗口移动到另一个窗口?

问题描述

我正在开发 WPF UI,现在我有一个登录窗口和一个 MainWindow,在 MainWindow 中有多个 userControls,现在当应用程序启动时它会打开登录窗口,我如何移动到 MainWindow 并关闭登录窗口我点击登录按钮??

标签: c#wpfwindows

解决方案


只需关闭登录窗口并打开主窗口。

例如。在按钮单击上

private void LoginButton_Click(object sender, EventArgs args)
{
     this.Close();
     (new MainWindow()).Open();
}

唯一要记住的是指定ShutdownMode的默认值是OnLastWindowClose

https://docs.microsoft.com/en-us/dotnet/api/system.windows.application.shutdownmode?view=netframework-4.8

如果您想坚持使用默认值ShutdownMode,您可以执行以下操作

private void LoginButton_Click(object sender, EventArgs args)
{
     this.Hide(); //first hide the LoginWindow
     (new MainWindow()).Open();
     this.Close(); //now close the LoginWindow, and the "MainWindow" will be the LastWindow
}

推荐阅读