我正在努力在ServicedComponent中托管WPF应用程序。我有一个WPF库,我需要从本机代码中使用它。为了实现这个目标,我创建了一个outproc COM+组件,将所有的WPF调用放入其中,并从本机代码中调用该组件,如下所示:
// Managed
[ComVisible(true)]
public class HpPcKeyboardSrv : ServicedComponent, ISrv
{
...
}
// Native
CComPtr<ISrv> spISrv;
hr = spISrv.CoCreateInstance(__uuidof(MySrv), nullptr, CLSCTX_SERVER);
ATLVERIFY(SUCCEEDED(hr));
hr = spISrv->WPFCommand();
ATLVERIFY(SUCCEEDED(hr));它作为一个原型完美地工作,但当我添加实际的WPF功能时,一切都开始瓦解。
由于臭名昭著的WPF异常"The calling thread must be STA, because many UI components require this",我无法在COM+ ServicedComponent中创建WPF窗口。其中一个解决方案是使用Dispatcher。问题是WPF调度程序Dispatcher.CurrentDispatcher不会调用BeginInvoke()中函数
public void WPFCommand()
{
Dispatcher.CurrentDispatcher.BeginInvoke((Action)delegate
{
System.Threading.ApartmentState aptStateLocal = Thread.CurrentThread.GetApartmentState();
Debug.WriteLine("Spawned thread apartment: {0}", aptStateLocal);
_Window = new Window();
});
}另一种选择是使用Application.Current.Dispatcher。这种方法的问题是,在此调用中,Application.Current为空,因此没有可用的调度程序。
好的。接下来要尝试的是在STA模型中产生威胁,如下所示:
public void WPFCommand()
{
if (Thread.CurrentThread.GetApartmentState() != ApartmentState.STA)
{
Thread thread = new Thread(() =>
{
System.Threading.ApartmentState aptState = Thread.CurrentThread.GetApartmentState();
Debug.WriteLine("Spawned thread apartment: {0}", aptState); // <- Still MTA
// !!! If _Window is the member of the class, the thread will be MTA
// !!! otherwise STA
_Window = new Window();
System.Windows.Threading.Dispatcher.Run();
});
Debug.WriteLine("Thread apartment state1: {0}", thread.GetApartmentState());
thread.SetApartmentState(ApartmentState.STA); // <- even though set as STA
Debug.WriteLine("Thread apartment state2: {0}", thread.GetApartmentState());
thread.IsBackground = true;
thread.Start();
thread.Join();
}
}这段代码在一定程度上有所帮助。因为已经设置为STA模型的生成线程,如果_Window是类成员(但如果不是,它就是STA ),则无论如何都会在MTA中被调用,因此new Window()会抛出相同的“必须是STA”异常。
在这一点上,我完全卡住了。如何在ServicedComponent中实际创建WPF元素?或者如何在本机代码和WPF之间进行交互?任何想法都是值得感谢的。
更新:奇怪的是,赋值(_Window =新窗口())会影响线程模型。如果_Window是类的成员,则踏步模型仍然是MTA。如果它是一个局部变量,则线程模型将更改为MTA。似乎应该以其他方式将_Window指定为类的成员。
发布于 2015-01-20 15:45:54
我可能在寻找解决方案的轨道上,或者完全偏离了轨道-
http://drwpf.com/blog/2007/10/05/managing-application-resources-when-wpf-is-hosted/
基本上,上述方法加载所有资源字典并创建WPF环境。请勾选“在代码中管理资源字典集合并在元素级别合并它们”。
因此,在此之后,您可以直接从WPFCommand调用您的窗口,而不必担心STA。
https://stackoverflow.com/questions/24226231
复制相似问题