WPF打开设置窗口从窗体应用程序

WPF打开设置窗口从窗体应用程序

问题描述:

可能重复:结果
通知时,WPF UI关闭

我问了一个很之前类似的问题,但我没有得到任何有用的回复。我希望有人在那里也许能帮助我。

I have asked a very similar question before, but i did not get any usable response. I am hoping that someone out there might be able to help me.

我有编程为一个Windows窗体应用程序的现有托盘应用程序。
我有现成的设置形式编程为WPF

I have an existing tray app programmed as a Windows Form app. I have an existing Settings form programmed as WPF.

托盘的应用程序需要能够:
*打开设置应用n次,只有一个实例,在一个时间
曾经打开*接收通知,当用户关闭设置应用程序。

The Tray App needs to be able to: * Open the Settings App n times, with only one instance ever opened at a time * Receive notification when the user closes the Settings App.

设置应用程序使用的ResourceDictionary,我相信手段它必须从App.xaml中启动。

The Settings App uses a ResourceDictionary, which I believe means it must be started from App.xaml.

我可以设置窗口打开一次,并通知当它使用下面的代码关闭托盘,但我不能得到它再次打开。

I can get the Settings window to open once and notify the Tray when it is shut down using the following code, but I cannot get it to open again.

        if (gui == null)
        {
            //shell = new mainWindow();
            //shell.CloseEvent += settings_FormClosed;

            gui = new App();
            //gui.ShutdownMode = ShutdownMode.OnExplicitShutdown;
            gui.MainWindow = shell;
            gui.InitializeComponent();
            //gui.Run();
            gui.Exit += new System.Windows.ExitEventHandler(settings_FormClosed);

            IsUIOpen = true;
        }

如果我去掉上面的线,我可以再拦截接近事件,设置能见度为隐藏。用户选择设置在接下来的时间,我可以将其设置回可见。然而,第一次设置打开,也不会触发退出事件,外壳没有得到所有的ReferenceDictionary(或失去对它的访问),所以窗口无法正常显示。

If I uncomment the lines above, I can then intercept the close event, and set the Visibility to Hidden. The next time the user selects Settings, I can set it back to Visible. However, the first time Settings opens, it will not trigger the Exit event and shell is not getting all the ReferenceDictionary (or losing access to it), so the window is not displaying properly.

有没有一种方法可以从应用程序运行的WPF和接入主窗口(这通常似乎是空,是我自己的窗口的类型不同),因此可以添加自定义的监听器的主窗口?

Is there a way to run WPF from the app, and access MainWindow (which often seems to be null, and is not of the type of my own window) so a custom listener can be added to mainWindow?

另外,有没有办法处置静态应用程序,这样我可以在下次用户点击设置重新初始化它。

Alternately, is there a way to dispose of the static App so I can re-instantiate it the next time the user clicks on Settings.

我知道一定有办法让WPF发挥好与Win形式。谢谢大家谁试图帮助。这一直让我头疼的大

I know there must be some way to get WPF to play nice with Win Forms. Thank you to everyone who attempts to help. This has been giving me major headaches.

由于您的设置是一个应用程序,我会用来自的System.Diagnostics.Process您的Winforms应用程序中开始你的WPF应用程序的设置。既然你已经开始设置为一个应用程序,而不是试图调用它作为一个窗口,您的样式将被加载。此外,您还可以使用过程中的WaitForExit()或WaitForExit(INT)方法来等待您的设置应用程序来完成,放弃控制权交还给Winforms应用程序的设置应用完成时才会出现。这也可作为当设置应用程序被关闭请求通知

Since your settings is an app, I would use the System.Diagnostics.Process from within your Winforms app to start your WPF settings app. Since you are starting the Settings as an application rather than trying to invoke it as a Window, your styles will be loaded. Also, you can use the WaitForExit() or WaitForExit(int) methods of Process to wait for your settings app to finish, relinquishing control back to your Winforms app only when the Settings app is complete. This also serves as the notification requested when the Settings app is closed.

下面是一个简单的代码SNIPPIT:

Here's a quick code snippit:

在情况下,打开设置WPF应用程序:

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



由主线程调用上WaitForExit的的WinForms应用程序被阻塞,等待的设置应用程序来完成。这将阻止用户打开多个设置窗口。我没有看到的Winforms应用程序非阻塞的要求。所以,如果你不能阻止,我能想出法子

By calling WaitForExit on the main thread, the WinForms app is blocked waiting for the settings app to complete. This will prevent users from opening more than one settings window. I didn't see a non-blocking requirement for the Winforms app. So if you can't block, I can come up with another way.

更新
要抓住执行的程序集的路径:

UPDATE To grab the executing assembly's path:

  string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );