[英]Closing all Windows in a C# WPF application
I'm creating a little WPF app in VS2013Express
and I've come across a little problem.我正在VS2013Express
创建一个小 WPF 应用程序, VS2013Express
我遇到了一个小问题。 You see, there are three windows, MainWindow
, LatAndLongDialog
, TimeCitiesDialog
.你看,有三个窗口, MainWindow
, LatAndLongDialog
, TimeCitiesDialog
。
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 them all?我怎样才能全部关闭它们? Please help!请帮忙!
The proper way to shutdown a WPF app is to use Application.Current.Shutdown()
.关闭 WPF 应用程序的正确方法是使用Application.Current.Shutdown()
。 This will close all open Window
s, raise some events so that cleanup code can be run, and it can't be canceled.这将关闭所有打开的Window
,引发一些事件,以便可以运行清理代码,并且无法取消。 Environment.Exit()
terminates the application immediately even if other threads are executing.即使其他线程正在执行, Environment.Exit()
也会立即终止应用程序。
You should also consider setting the Owner
on non-main Window
s.您还应该考虑在非主Window
上设置Owner
。 The behavior will likely be more like what you would expect in regards to Z-order, minimizing, and maximizing.该行为可能更像您对 Z 顺序、最小化和最大化的期望。 As an added bonus, the owned windows will automatically close when the owner Window
closes.作为额外的奖励,当所有者Window
关闭时,拥有的窗口将自动关闭。
private void CloseAllWindows()
{
for (int intCounter = App.Current.Windows.Count - 1; intCounter >= 0; intCounter--)
App.Current.Windows[intCounter].Close();
}
Close all opened current windows.关闭所有打开的当前窗口。
Use this instead this.Close()
使用 this 代替this.Close()
Environment.Exit(0);
this will force everything to close这将迫使一切关闭
If you keep track of the Dialogs outside of the scope of the methods you use to open them, you can call which ever methods on those Dialogs you wish from anywhere within the Class.如果您在用于打开它们的方法范围之外跟踪对话框,则可以从类中的任何位置调用您希望的那些对话框上的任何方法。 Here I have them as Class variables and they are instantiated there but not shown until you press the buttons.在这里,我将它们作为类变量,它们在那里被实例化,但在您按下按钮之前不会显示。 You can also create "Close" buttons for those specific windows and call their .Close()
methods when ever you wish.您还可以为这些特定窗口创建“关闭”按钮,并在需要时调用它们的.Close()
方法。 That will allow you to open and close them at will.这将允许您随意打开和关闭它们。 You can also call their .Close()
methods when the main form closes.您还可以在主窗体关闭时调用它们的.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();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.