从Windows Forms应用程序在另一个线程中打开WPF窗口 [英] Opening a WPF Window in another Thread from a Windows Forms App

查看:113
本文介绍了从Windows Forms应用程序在另一个线程中打开WPF窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我首先要说我不确定这是否一定是解决这个问题的正确方法,但是...

OK, I should start this off by saying I'm not sure this is necessarily the right way to deal with this but...

基本上,我已经在WPF中创建了一个窗口,用于在应用程序中显示通知(有点像Outlook New Mail通知).我想尝试在自己的线程中显示此窗口(将来可能会做一些工作).

Basically I have created a Window in WPF for displaying notifications in my application (a bit like Outlook New Mail notification). I would like to try and show this Window in it's own thread (it might do some work in the future).

我已经使用WPF创建了Window,因为它在处理AlwaysOnTop和Fading In and Out之类的功能方面要好一些.

I've created the Window using WPF because it's a bit nicer at handling things like AlwaysOnTop and Fading In and Out.

显示通知的应用程序是3.5 Windows Forms应用程序.我看过类似此 SOF的示例:C#winforms启动(启动)表单未隐藏,用于在其中显示表单一个不同的线程,但是我无法使用WPF窗口启动新的消息循环.

The application that shows the notification is a 3.5 Windows Forms App. I've seen examples similar to this SOF: C# winforms startup (Splash) form not hiding for showing a Form in a different thread however I'm unable to start a new Message Loop with the WPF Window.

我已经尝试过在新线程中调用Notification.Show(),但是Window从不显示.

I've tried just calling Notification.Show() in a new thread however the Window never displays.

  1. 是否可以在自己的线程中显示此WPF?

  1. Is it possible to show this WPF in it's own thread?

为什么我看到一些资源说您不应在单独的线程中显示任何UI?

Why do I see some resources say that you should not show any UI in a separate thread?

推荐答案

The WPF Threading Model has details on this. Look for "Multiple Windows, Multiple Threads."

基本上,您启动一​​个新线程,该线程创建相关窗口,然后调用

Basically, you start a new thread, which creates the relevant window and then calls

System.Windows.Threading.Dispatcher.Run();

为了在新线程上为该窗口运行调度程序.

in order to run a dispatcher for that window, on the new thread.

这篇关于从Windows Forms应用程序在另一个线程中打开WPF窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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