关闭 C# WPF 应用程序中的所有窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37063887/
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 13:47:48  来源:igfitidea点击:

Closing all Windows in a C# WPF application

c#.netwpf

提问by Aravind Suresh

I'm creating a little WPF app in VS2013Express and I've come across a little problem. You see, there are three windows, MainWindow, LatAndLongDialog, TimeCitiesDialog.

我正在 VS2013Express 中创建一个小 WPF 应用程序,但我遇到了一个小问题。你看,有三个窗口,MainWindow, LatAndLongDialog, TimeCitiesDialog

LatAndLongDialogand TimeCitiesDialogare opened from MainWindow (with the click of a button). I want all the other windows to close when the Closed()event is called on MainWindow. The code on MainWindow.xaml.cs:

LatAndLongDialogTimeCitiesDialog从 MainWindow 打开(单击按钮)。我希望在Closed()调用事件时关闭所有其他窗口MainWindow。MainWindow.xaml.cs 上的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GlobalTime_ELITE_for_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();


            UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" +
                                 "to view the World Time there. Or, select another one of the\n" +
                                 "options below to do that. Go to Help by clicking on the link\n" +
                                 "on the upper-right corner of the window to view everything you\n" +
                                 "can do.";
            this.Closed += CloseOff;
        }

        private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
        {
            TimeCitiesDialog ObjectReference = new TimeCitiesDialog();
            ObjectReference.Show();
        }

        private void OpenLatAndLongDialog(Object Sender, EventArgs E)
        {
            LatAndLongDialog ObjectReference = new LatAndLongDialog();
            ObjectReference.Show();
        }

        private void CloseOff(Object Sender, EventArgs E)
        {
            this.Close();

            TimeCitiesDialog tcdor = new TimeCitiesDialog();
            LatAndLongDialog laldor = new LatAndLongDialog();


        }
    }
}

How can I close 'em all? Please help!

我怎样才能全部关闭?请帮忙!

回答by James Durda

The proper way to shutdown a WPF app is to use Application.Current.Shutdown(). This will close all open Windows, raise some events so that cleanup code can be run, and it can't be canceled. Environment.Exit()terminates the application immediatelyeven if other threads are executing.

关闭 WPF 应用程序的正确方法是使用Application.Current.Shutdown(). 这将关闭所有 open Windows,引发一些事件,以便可以运行清理代码,并且不能取消。 即使其他线程正在执行,也会立即Environment.Exit()终止应用程序。

You should also consider setting the Owneron non-main Windows. The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing. As an added bonus, the owned windows will automatically close when the owner Windowcloses.

您还应该考虑Owner在非主Windows上设置。该行为可能更像您对 Z 顺序、最小化和最大化的期望。作为额外的奖励,当所有者Window关闭时,拥有的窗口将自动关闭。

回答by user2960398

private void CloseAllWindows()
      {
         for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
            App.Current.Windows[intCounter].Close();
      }

Close all opened current windows.

关闭所有打开的当前窗口。

回答by Mostafiz

Use this instead this.Close()

改用这个 this.Close()

Environment.Exit(0);

this will force everything to close

这将迫使一切关闭

回答by Mikanikal

If you keep track of the Dialogs outside of the scope of the methods you use to open them, you can call which ever methods on those Dialogs you wish from anywhere within the Class. Here I have them as Class variables and they are instantiated there but not shown until you press the buttons. You can also create "Close" buttons for those specific windows and call their .Close()methods when ever you wish. That will allow you to open and close them at will. You can also call their .Close()methods when the main form closes.

如果您在用于打开它们的方法范围之外跟踪对话框,则可以从类中的任何位置调用您希望的那些对话框上的任何方法。在这里,我将它们作为类变量,它们在那里被实例化,但在您按下按钮之前不会显示。您还可以为这些特定窗口创建“关闭”按钮,并在需要.Close()时调用它们的方法。这将允许您随意打开和关闭它们。您还可以.Close()在主窗体关闭时调用它们的方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace GlobalTime_ELITE_for_WPF
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        TimeCitiesDialog tcDialog = new TimeCitiesDialog();
        LatAndLongDialog lalDialog = new LatAndLongDialog();

        public MainWindow()
        {
            InitializeComponent();    

            UserDescText.Content = "Select a TimeCity or enter the latitude and longitude in \n" +
                                 "to view the World Time there. Or, select another one of the\n" +
                                 "options below to do that. Go to Help by clicking on the link\n" +
                                 "on the upper-right corner of the window to view everything you\n" +
                                 "can do.";
            this.Closed += CloseOff;
        }

        private void OpenTimeCitiesDialog(Object Sender, EventArgs E)
        {                
            tcDialog.Show();
        }

        private void OpenLatAndLongDialog(Object Sender, EventArgs E)
        {                
            lalDialog.Show();
        }

        private void CloseOff(Object Sender, EventArgs E)
        {
            // Close the dialogs first, then allow this method
            // to end which will finish the this.Close() process.
            tcDialog.Close();
            lalDialog.Close();
        }
    }
}