【发布时间】:2011-04-06 15:31:35
【问题描述】:
如何在我的应用程序中创建的表单上托管在不同 AppDomain 中创建的 WinForms Control(或 WPF Window)?
我正在尝试创建一个复合 UI 应用程序,它是插件的无功能外壳/主机,这些插件完全在它们自己的应用程序域中运行,因此我希望能够从这些插件中获取 UserControl 对象为“在宿主进程的主窗体上的某种容器控件中托管”。
我以为我已经破解了它,但由于 appdomains 之间的对话涉及 MarshallByRefObject 代理,我无法采用我的第一个原型方法:
shellForm.Panel1.Controls.Add(proxyObjectForUserControlInOtherAssembly);
我已经在问题的末尾发布了我从中得到的异常。
我确实尝试过直接获得控件:
var ctl = Control.FromHandle(proxyObjectForUserControlInOtherAssembly.Handle);
shellForm.Panel1.Controls.Add(ctl);
这给出了可预测的结果。我的 hWnd 没问题,但 Control.FromHandle() 返回 null。大概是因为句柄是在不同的 appdomain 上创建的。
如果我在这方面无可救药地偏离了轨道,我将非常感谢朝正确的方向戳。 :)
非常感谢。
System.Runtime.Remoting.RemotingException was unhandled by user code
Message=Remoting cannot find field 'parent' on type 'System.Windows.Forms.Control'.
Source=mscorlib
StackTrace:
at System.Object.GetFieldInfo(String typeName, String fieldName)
at System.Object.FieldGetter(String typeName, String fieldName, Object& val)
at System.Object.FieldGetter(String typeName, String fieldName, Object& val)
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at AppDomainTest.Shell.Form1.Display(Control control) in c:\temp\AppDomainTest\AppDomainTest\AppDomainTest.Shell\Form1.cs:line 24
at System.Runtime.Remoting.Messaging.StackBuilderSink._PrivateProcessMessage(IntPtr md, Object[] args, Object server, Int32 methodPtr, Boolean fExecuteInContext, Object[]& outArgs)
at System.Runtime.Remoting.Messaging.StackBuilderSink.SyncProcessMessage(IMessage msg, Int32 methodPtr, Boolean fExecuteInContext)
InnerException:
【问题讨论】:
标签: c# .net winforms remoting appdomain