WPF 从主窗口关闭所有窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17739963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:14:35  来源:igfitidea点击:

WPF close all window from the main window

c#wpfxaml

提问by Maverick

I have login window. From this login window, i am intializing the main window. Once login successfully happened, i close the login window. Now i am having two other windows, which i am calling from Main Window. Once i close the main Window, I am able to close the other two windows as well as Main Window. But program still runs in memory. I have to close it manually from the Visual Studio. How should i close the Program all instances fully?? This is the Main window Close Event code.

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

private void usimClose(object sender, EventArgs e)
{
   newScreen2.Close();
   newScreen3.Close();  
   this.Close();                        
}

This is my Login Window Code. Once the user click on the submit button.

这是我的登录窗口代码。一旦用户点击提交按钮。

 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;
            }
        }
}

回答by Koen

Try Application.Current.Shutdown();

尝试 Application.Current.Shutdown();

From MSDN

来自MSDN

Calling Shutdown explicitly causes an application to shut down, regardless of the ShutdownMode setting. However, if ShutdownMode is set to OnExplicitShutdown, you must call Shutdown to shut down an application.

Important note

When Shutdown is called, the application will shut down irrespective of whether the Closing event of any open windows is canceled.

This method can be called only from the thread that created the Application object.

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

重要的提示

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

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

回答by Ankit Jain

You can close all windows using this

您可以使用此关闭所有窗口

App.Current.Shutdown();

or

或者

you can manually close it

你可以手动关闭它

Window parentwin = Window.GetWindow();
parentwin.Close();

回答by chris.ellis

If you starting point is your MainWindow, then just start there.

如果您的起点是 MainWindow,那么就从那里开始。

Firstly, host the LoginForm in your MainWindow, and show it using ShowDialog() to force the user to interact with the LoginForm. Return the result of a successful/unsuccessful interaction to the MainForm.

首先,在您的 MainWindow 中托管 LoginForm,并使用 ShowDialog() 显示它以强制用户与 LoginForm 交互。将成功/不成功交互的结果返回到 MainForm。

    private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
    {
        var form = new LoginForm();
        var result = form.ShowDialog();

        if (result ?? false)
        {
            // Carry on opening up other windows
        }
        else
        {
            // Show some kind of error message to the user and shut down
        }
    }

Otherwise, technically your LoginForm is hosting your MainForm which is, frankly, odd.

否则,从技术上讲,您的 LoginForm 正在托管您的 MainForm,坦率地说,这很奇怪。

回答by Erno

Have a look at my answer here: How to close wpf window from another project

在这里看看我的回答:How to close wpf window from another project

An Application.Current.Shutdown() will stop the application in a very abrupt way.

Application.Current.Shutdown() 将以非常突然的方式停止应用程序。

It is better to gracefully keep track of the windows and close them.

最好优雅地跟踪窗口并关闭它们。