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

atmip9wb  于 2023-01-27  发布在  C#
关注(0)|答案(4)|浏览(397)

我正在VS2013Express中创建一个小的WPF应用程序,我遇到了一个小问题。你看,有三个窗口,MainWindowLatAndLongDialogTimeCitiesDialog
“Lat 'AndLongDialog”和“TimeCitiesDialog "从主窗口打开(单击按钮)。我希望在主窗口调用" Closed()”事件时关闭所有其他窗口。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();
        }
    }
}

我怎么才能把它们都关上?请帮帮忙!

siotufzp

siotufzp1#

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

gijlo24d

gijlo24d2#

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

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

von4xj4u

von4xj4u3#

请改用this.Close()

Environment.Exit(0);

这将强制关闭所有内容

oyt4ldly

oyt4ldly4#

如果您在用于打开对话框的方法的作用域之外跟踪对话框,你可以在类中的任何地方调用对话框上的任何方法。2这里我把它们作为类变量,它们在那里被示例化,但直到你按下按钮才显示出来。3你也可以为那些特定的窗口创建“关闭”按钮,并在你愿意的时候调用它们的.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();
        }
    }
}

相关问题