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

7
我正在使用VS2013Express创建一个小型WPF应用程序,遇到了一点问题。 有三个窗口,分别是MainWindow、LatAndLongDialog和TimeCitiesDialog。
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();
        }
    }
}

我该如何关闭它们?请帮忙!

你为什么要在 CloseOff() 函数内实例化新的 TimeCitiesDialogLatAndLongDialog - user585968
4个回答

16
关闭WPF应用程序的正确方法是使用Application.Current.Shutdown()。这将关闭所有打开的Window,引发一些事件以便可以运行清理代码,并且无法取消。 Environment.Exit()立即终止应用程序,即使其他线程正在执行。
您还应该考虑在非主要Window上设置Owner。行为可能更像您对Z顺序、最小化和最大化的期望。作为额外的奖励,当所有者Window关闭时,所拥有的窗口将自动关闭。

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

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

3
请使用this.Close()代替。
Environment.Exit(0);

这将强制关闭一切。


请再详细解释一下。 - Aravind Suresh Thakidayil
虽然这将关闭应用程序中的每个窗口,但OP并没有说他们想退出应用程序。这将退出整个应用程序。OP,请考虑一下这不是启动窗体的情况,你只想从另一个窗体关闭一个窗体的逻辑。您必须在创建或显示它的方法范围之外保留引用。请查看我的答案以了解该技术。 - Mikanikal
1
@Mikanikal 不正确。考虑到 MainWindow 已经在关闭过程中,而 OP 想要关闭所有其他窗口,Windows 将关闭应用程序,因为当前进程没有其他窗口保持打开状态。因此,最终结果是 OP 希望程序终止 - user585968
是的,OP希望一切都结束,也就是程序终止。 - Mostafiz
1
Environment.Exit 是突然关闭应用程序并立即退出。Application.Current.Shutdown() 以有序的方式关闭。 - James Durda

1
如果您在打开对话框的方法范围之外跟踪对话框,您可以从类的任何位置调用那些对话框上的任何方法。在这里,我将它们作为类变量,并在那里实例化,但在按下按钮之前不显示它们。您还可以为这些特定窗口创建“关闭”按钮,并在需要时调用其.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();
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接