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

阿拉维德·苏雷什

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

LatAndLongDialogTimeCitiesDialog从MainWindow中打开(单击按钮)。我希望在Closed()调用事件时关闭所有其他窗口MainWindowMainWindow.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();


        }
    }
}

我该如何全部关闭?请帮忙!

詹姆斯·达达(James Durda)

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

您还应该考虑将Owneron设置为non-main Window该行为可能更像您对Z阶,最小化和最大化所期望的行为。作为一项额外的奖励,拥有者Window关闭时,拥有的窗户将自动关闭。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在C#WPF应用程序中处理线程

在C#WPF或Windows Forms应用程序中使用DirectX c ++ DLL

C#WPF MVVM应用程序的启动顺序

C#WPF应用程序Webbrowser无法运行angularjs

c#WPF如何延迟应用程序的自动启动?

C#WPF应用程序:什么是“这”?

c#wpf应用程序中的两个xml请求

将样式表链接到C#WPF应用程序中的本地html页面

如何在c#WPF应用程序中取消上传

在WPF应用程序中编辑所有标签颜色

关闭所有意图并关闭应用程序

带有MahApps的C#WPF应用无法在Windows 2008中显示

如何更改 C# Windows 应用程序中的所有表单属性

所有 X 值点在图表 c# windows 应用程序中不可见

C#WPF应用程序在Visual中启动,但在直接运行时无法启动

在修改C#WPF应用程序中的binded属性之前,如何确保与UI同步基于绑定的样式更改?

无法将图像添加到C#WPF .NetCore WPF应用程序

如何知道与Windows服务相对应的所有应用程序是否已关闭?

在Windows 7关闭过程中调用WPF应用程序

`sizeof` C ++应用程序中的所有类型

在C#WPF应用程序中使用外部C ++ DLL

使用Electron时关闭所有应用程序窗口

侏儒:如何关闭所有应用程序?

如何完成所有活动并关闭android中的应用程序?

屏幕锁定会关闭17.10中的所有应用程序

在WPF中关闭事件后保持应用程序运行

C#WPF应用程序耗时任务的更新进度

如何使用C#WPF应用程序流式传输图像

如何混淆使用本地化扩展的C#wpf应用程序