2012-10-02 40 views
1

我从托盘应用程序打开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.Deactivatedgui.SessionEndinggui.MainWindow.Closinggui.MainWindow.Deactivated,而且很可能一些人。

当用户关闭窗口,这个代码是从Shell.xaml称为:

private void Cancel_Click(object sender, RoutedEventArgs e) 
    { 
     presenter.Close(); 
     this.Close(); 
    } 

我意识到应用程序是静态的,所以它永远不会接近,但这些事件处理程序的一个应该钩我来结束活动。

在情况下,它是有用的,流程如下:TrayApp.cs - >的App.xaml - > Shell.xaml

任何建议,将不胜感激。提前致谢。

回答

0

乔希能够给出正确的解决方案。你可以看到他的回答here

基本上,我需要启动WPF作为一个单独的进程,然后使用MyProcess.WaitForEnd()调用。我将它添加到一个线程中,所以它不会阻塞托盘。代码如下:

Process myProcess = new Process(); 
myProcess.StartInfo.UseShellExecute = false; 
myProcess.StartInfo.FileName = "C:\\mysettingsapp\\mysettingsapp.exe"; // replace with path to your settings app 
myProcess.StartInfo.CreateNoWindow = false; 
myProcess.Start(); 
// the process is started, now wait for it to finish 
myProcess.WaitForExit(); // use WaitForExit(int) to establish a timeout 
1

您应该尝试Closing事件。 This article提供了有关WPF何时实际关闭的有用信息(不仅仅是窗口)。

+0

谢谢你的回应,达米安,但我已经尝试了关闭事件。当我关闭用户界面时,它似乎不会启动。奇怪的是,当我关闭整个应用程序时它也不会启动。 – Tim

+0

嗯,请帮忙吗? 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关门事件感燃煤换系统关闭 –