【发布时间】:2022-01-05 18:03:00
【问题描述】:
我正在开发 WPF UI,现在我有一个登录窗口和一个 MainWindow,在 MainWindow 中有多个 userControls,现在当应用程序启动时它会打开登录窗口,我如何移动到 MainWindow 并关闭点击登录按钮时的登录窗口??
【问题讨论】:
我正在开发 WPF UI,现在我有一个登录窗口和一个 MainWindow,在 MainWindow 中有多个 userControls,现在当应用程序启动时它会打开登录窗口,我如何移动到 MainWindow 并关闭点击登录按钮时的登录窗口??
【问题讨论】:
只需关闭登录窗口并打开主窗口。
例如。点击按钮
private void LoginButton_Click(object sender, EventArgs args)
{
this.Close();
(new MainWindow()).Open();
}
唯一要记住的是指定ShutdownMode默认是OnLastWindowClose
如果您想坚持使用默认的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
}
【讨论】: