WPF关闭主窗口中的所有窗口

Mav*_*ick 0 c# wpf xaml

我有登录窗口.从这个登录窗口,我正在初始化主窗口.登录成功后,我关闭登录窗口.现在我有另外两个窗口,我从主窗口调用.一旦我关闭主窗口,我就可以关闭另外两个窗口以及主窗口.但程序仍在内存中运行.我必须从Visual Studio手动关闭它.我该如何完全关闭程序的所有实例?这是主窗口关闭事件代码.

private void usimClose(object sender, EventArgs e)
{
   newScreen2.Close();
   newScreen3.Close();  
   this.Close();                        
}
Run Code Online (Sandbox Code Playgroud)

这是我的登录窗口代码.用户点击提交按钮后.

 private void btnLogin_Click(object sender, RoutedEventArgs e)
 {
        if (txtUserName.Text.Length == 0)
        {
            errormessage.Text = "Please Enter UserName";
            txtUserName.Focus();
        }
        else
        {                
            LoginStatus _status = _Login.LoginUsimgClient(txtUserName.Text, txtPassword.Password.ToString());

            if (_status.BoolLoginstatus)
            {
                mainWindow.RunApplication();
                string struserName = _status.StringUserFirstName;
                mainWindow.userName.Text = "Welcome " + struserName;
                mainWindow.Show();
                this.Close();                    
            }
            else
            {
                errormessage.Text = _status.StringErrorDescription;
                txtUserName.Text = String.Empty;
                txtPassword.Password = String.Empty;
            }
        }
}
Run Code Online (Sandbox Code Playgroud)

Koe*_*oen 6

尝试 Application.Current.Shutdown();

来自MSDN

无论ShutdownMode设置如何,调用Shutdown都会显式导致应用程序关闭.但是,如果ShutdownMode设置为OnExplicitShutdown,则必须调用Shutdown来关闭应用程序.

重要的提示

当调用Shutdown时,无论是否取消任何打开窗口的Closing事件,应用程序都将关闭.

只能从创建Application对象的线程调用此方法.