WPF UI关闭时通知 [英] Notification when WPF UI closes
问题描述
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();
}
我意识到App是静态的,所以它永远不会关闭,但其中之一事件处理程序应该挂起我关闭的事件。
如果它很有用,流程如下:TrayApp.cs - > App.xaml - > Shell.xaml
任何建议将不胜感激。感谢提前。
Josh能够给出正确的解决方案。您可以看到他的答案此处。
基本上,我需要作为一个单独的进程启动WPF,然后使用MyProcess.WaitForEnd()调用。我把它添加到一个线程,所以它不会阻止托盘。代码如下:
处理myProcess = new Process();
myProcess.StartInfo.UseShellExecute = false;
myProcess.StartInfo.FileName =C:\\\mysettingsapp\\\mysettingsapp.exe; //替换为您的设置应用程序的路径
myProcess.StartInfo.CreateNoWindow = false;
myProcess.Start();
//进程启动,现在等待它完成
myProcess.WaitForExit(); //使用WaitForExit(int)建立超时
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.
Josh was able to give the correct solution. You can see his answer here.
Basically, I needed to start the WPF as a separate process, and then use the MyProcess.WaitForEnd() call. I added this to a thread so it wouldn't block the Tray. The code is as follows:
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
这篇关于WPF UI关闭时通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!