我从托盘应用程序打开WPF窗口。我用下面的代码打开窗口:WPF UI关闭时的通知
if (gui == null)
{
gui = new App();
gui.MainWindow = new mainWindow();
gui.InitializeComponent();
IsUIOpen = true;
}
else if (!IsUIOpen)
{
gui.InitializeComponent();
gui.MainWindow.Show();
gui.MainWindow = new mainWindow();
IsUIOpen = true;
}
我需要从App级别运行UI,因为它使用资源字典。问题是,当用户关闭窗口时,我需要运行代码,但没有一个事件处理程序似乎在通知我。
我曾尝试以下:
gui.Exit += new System.Windows.ExitEventHandler(settings_FormClosed);
gui.MainWindow.Closed += new EventHandler(settings_FormClosed);
我也曾尝试gui.Deactivated
,gui.SessionEnding
,gui.MainWindow.Closing
,gui.MainWindow.Deactivated
,而且很可能一些人。
当用户关闭窗口,这个代码是从Shell.xaml称为:
private void Cancel_Click(object sender, RoutedEventArgs e)
{
presenter.Close();
this.Close();
}
我意识到应用程序是静态的,所以它永远不会接近,但这些事件处理程序的一个应该钩我来结束活动。
在情况下,它是有用的,流程如下:TrayApp.cs - >的App.xaml - > Shell.xaml
任何建议,将不胜感激。提前致谢。
谢谢你的回应,达米安,但我已经尝试了关闭事件。当我关闭用户界面时,它似乎不会启动。奇怪的是,当我关闭整个应用程序时它也不会启动。 – Tim
嗯,请帮忙吗? http://stackoverflow.com/questions/8908276/why-does-unloaded-event-of-window-do-not-fire-in-wpf http://stackoverflow.com/questions/3183729/why-isnt-my -wpf关门事件感燃煤换系统关闭 –