I am opening a WPF window from a tray app. I use the following code to open the window:
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;
}
I need to run the UI from the App level because it uses a Resource Dictionary. The problem is, I need to run code when the window is closed by the user, but none of the event handlers seem to be notifying me.
I have tried the following:
gui.Exit += new System.Windows.ExitEventHandler(settings_FormClosed);
gui.MainWindow.Closed += new EventHandler(settings_FormClosed);
I have also tried gui.Deactivated
, gui.SessionEnding
, gui.MainWindow.Closing
, gui.MainWindow.Deactivated
, and probably some others.
When the user closes the window, this code is called from Shell.xaml:
private void Cancel_Click(object sender, RoutedEventArgs e)
{
presenter.Close();
this.Close();
}
I realize App is static, so it will never close, but one of these event handlers should hook me up to a closing event.
In case it is useful, flow is as follows: TrayApp.cs -> App.xaml -> Shell.xaml
Any suggestions would be appreciated. Thanks in advance.