【问题标题】:Obtain reference to a UserControl from a different appdomain (composite UI)从不同的 appdomain(复合 UI)获取对 UserControl 的引用
【发布时间】: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


    【解决方案1】:

    一位同事帮助我指出了一个可能的方向:.NET Add-in Framework

    出于我的目的,该页面的关键点如下:

    这些段的程序集是 不需要在同一个 应用领域。您可以加载一个 添加到自己的新应用程序中 域,进入现有应用程序 域,甚至进入主机的 应用领域。你可以加载 多个加载项到同一个 应用程序域,这使 加载项共享资源和 安全上下文。

    【讨论】:

    • 嗨,尼尔。您最终是否使用了 .Net AddIn 框架?从阅读的内容来看,可以托管在不同的 AppDomain 中创建的 WPF 窗口(WPF 附带已经实现的适配器),但对于 Windows 窗体是不可能的。您是否尝试创建自己的适配器来传递 WinForms 控件?
    猜你喜欢
    • 2012-06-03
    • 1970-01-01
    • 2015-02-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多