在C#WPF应用程序中关闭所有Windows [英] Closing all Windows in a C# WPF application

查看:31
本文介绍了在C#WPF应用程序中关闭所有Windows的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在VS2013Express中创建一个小的WPF应用程序,但遇到了一个小问题.您会看到三个窗口,分别是 MainWindow LatAndLongDialog TimeCitiesDialog .

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.

LatAndLongDialog TimeCitiesDialog .我希望在 MainWindow 上调用 Closed()事件时,所有其他窗口都关闭.MainWindow.xaml.cs上的代码:

LatAndLongDialog and TimeCitiesDialog are 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:

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!

推荐答案

关闭WPF应用程序的正确方法是使用 Environment.Exit() 即使正在执行其他线程,也会立即终止应用程序.

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 immediately even if other threads are executing.

您还应该考虑设置

You should also consider setting the Owner on 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 Window closes.

这篇关于在C#WPF应用程序中关闭所有Windows的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆