2011-04-06 55 views
3

如何在我的應用程序中創建的表單上爲不同的AppDomain創建一個WinForms Control(或WPF Window)?從另一個應用程序域(合成UI)獲取對UserControl的引用

我想創建一個複合UI應用程序,這是一個無插件shell /主機插件,其中這些插件完全運行在他們自己的應用程序域中,所以我希望能夠從這些插件獲取UserControl對象「託管」在主機進程的主要形式中的某種容器控制。

我想我破解了,但因爲應用程序域之間的對話涉及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: 

回答

4

我一直在一個可能的方向由同事幫忙,指出:.NET Add-in Framework

從該頁面的關鍵點,我的目的如下:

的組件。這些段是 不需要在同一個 應用程序域中。您可以將 加載項加載到其自己的新應用程序 域中,加載到現有的應用程序 域中,或者甚至加載到主機的 應用程序域中。您可以將 多個加載項加載到同一個 應用程序域中,從而使 加載項可共享資源和 安全性上下文。

+0

嗨尼爾。你最終使用.Net AddIn框架?從讀過的東西可以承載在不同的AppDomain中創建的WPF窗口(WPF自帶的已經實現的適配器),但Windows Forms不可能。您是否嘗試創建自己的適配器來傳遞WinForms控件? – 2014-01-26 12:20:14

相關問題