【问题标题】:How to move from one window to another in WPF?如何在 WPF 中从一个窗口移动到另一个窗口?
【发布时间】:2022-01-05 18:03:00
【问题描述】:

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

【问题讨论】:

    标签: c# wpf windows


    【解决方案1】:

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

    例如。点击按钮

    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
    }
    

    【讨论】:

    • 非常感谢您的详细回答,成功了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2010-11-20
    • 2013-10-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    相关资源
    最近更新 更多