我正在VS2013Express中创建一个小的WPF应用程序,但遇到了一个小问题。你看,有三个窗口,MainWindow
,LatAndLongDialog
,TimeCitiesDialog
。
LatAndLongDialog
并TimeCitiesDialog
从MainWindow中打开(单击按钮)。我希望在Closed()
调用该事件时关闭所有其他窗口MainWindow
。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()
。这将关闭所有open Window
,引发一些事件,以便可以运行清除代码,并且无法取消清除代码。即使正在执行其他线程,也会立即Environment.Exit()
终止应用程序。
您还应该考虑将Owner
on设置为non-main Window
。该行为可能更像您对Z阶,最小化和最大化所期望的行为。作为一项额外的奖励,拥有者Window
关闭时,拥有的窗户将自动关闭。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句