通知时,WPF UI关闭 [英] Notification when WPF UI closes

查看:124
本文介绍了通知时,WPF UI关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从一个托盘的应用程序打开一个WPF窗口。我用下面的代码打开的窗口:

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;
        }



我需要,因为它使用资源词典运行从应用层面的UI 。问题是,我需要运行的代码,当窗口被用户关闭,但没有任何事件处理程序似乎是通知我。

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.

我曾尝试以下

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 ,也许一些人

I have also tried gui.Deactivated, gui.SessionEnding, gui.MainWindow.Closing, gui.MainWindow.Deactivated, and probably some others.

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

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.

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

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.

基本上,我需要启动WPF作为一个单独的进程,然后使用MyProcess.WaitForEnd()调用。我加入这一个线程,这样它就不会阻止托盘。代码如下:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆