c# - 关闭 C# WPF 应用程序中的所有 Windows

标签 c# .net wpf

我在 VS2013Express 中创建了一个小 WPF 应用程序,我遇到了一个小问题。 你看,一共有三个窗口,MainWindowLatAndLongDialogTimeCitiesDialog

`LatAndLongDialog` 和 `TimeCitiesDialog` 从 MainWindow 打开(点击按钮)。 我希望在“MainWindow”上调用“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();
        }
    }
}

如何关闭它们?请帮忙!

最佳答案

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

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

关于c# - 关闭 C# WPF 应用程序中的所有 Windows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37063887/

相关文章:

c# - 有没有更好的方法来实现 Shift+Tab 或减少缩进?

c# - 如何从 .NET 4.5 中的并行任务中产生

c# - 是否有用于开发 Entity Framework 4 代码优先的任何好的资源?

C# 在离开 accept_button 事件之前进行验证

.net - 是否应该在生产应用程序中使用 MEF

c# - 将 Windows 任务栏设置为前台窗口/进程

c# - 为什么实例化时 Unity Prefab 值错误?

c# - MVC 5 中的 System.Data.Entity.Core.ProviderIncompatible 异常

c# - 在应用程序的某些部分需要完全并行的情况下,F# 是否优于 C#?

WPF 双动画 : animate in steps?