退出HomeWindow到MainWindow

问题描述 投票:1回答:1

我有两个wpf窗口:用户将登录的MainWindow.xaml和成功登录时显示的Homewindow.xaml。 HomeWndow.xaml中的菜单项具有“注销”选项。当用户退出HomeWindow时,我想显示MainWindow以重新启动登录过程。在AppLogOut_Click按钮内,如果我使用this.Close()它关闭MainWindow.xaml和HomeWindow.xaml,如果我使用App.Current.Mainwindow.Close(),它不会关闭HomeWindow并打开MainWindow。这是我的代码。任何帮助将不胜感激。

private void HmWindow_Closing(object sender, CancelEventArgs e)
{
    MessageBoxResult result = MessageBox.Show(this, "Are you sure you want to exit?", "Confirm!", MessageBoxButton.YesNo, MessageBoxImage.Question);
    if (result == MessageBoxResult.Yes)
    {
        Application.Current.Shutdown();
    }
}

private void AppLogOut_Click(object sender, RoutedEventArgs e)
{
    MainWindow mw = new MainWindow();
    mw.InitializeComponent();
    mw.Show();
    //this.Close();
    App.Current.MainWindow.Close();
}

private void AppExit_Click(object sender, RoutedEventArgs e)
{
    Application.Current.Shutdown();
}
c# wpf
1个回答
0
投票

这一切都取决于ShutdownMode。默认值为OnLastWindowClosed,如果关闭最后打开的窗口,将强制应用程序关闭。

© www.soinside.com 2019 - 2024. All rights reserved.