I have a number of dll assemblies with usercontrols that I load into a Border control in my main window (like old MDI approach). I wish to now load these assemblies into a separate AppDomain. I have been unable to get events raised in the usercontrols to be received by the parent AppDomain.
What I currently have (working well)
Dim CurrentAssembly As Assembly = Reflection.Assembly.Load("SimpleAss")
MenuFeatureUC = CurrentAssembly.GetType("SimpleNS.UserControl1")
Dim CurrentInstance As FrameworkElement = Activator.CreateInstance(MenuFeatureUC)
ContentHostBrDr.Child = CurrentInstance
The event handlers, that the UserContrtols can raise to –for example-disable buttons in the parent window, are like:
Dim Events = CurrentInstance.GetType().GetEvents()
Dim DisableHandlerMethod = Me.GetType().GetMethod("DisableHandler")
For Each ev In Events
If ev.Name = "DisableMethodInParent" Then`
ev.AddEventHandler(obj, [Delegate].CreateDelegate(ev.EventHandlerType, Me, DisableHandlerMethod))
End If
Next
Public Sub DisableHandler(ByVal sender As Object, ByVal e As EventArgs)
MainWindowButton.IsEnabled=false
End Sub
The UserControls raise these events as required.
Public Class UserControl1
Public Event DisableMethodInParent As EventHandler
Private Sub RaiseDisableEventBut_Click(sender As Object, e As RoutedEventArgs) Handles RaiseDisableEventBut.Click
RaiseEvent DisableMethodInParent(Me, EventArgs.Empty)
End Sub
End Class
What I CAN do I can load the Usercontrols into a separate AppDomain using this code. It works well.
Dim domaininfo As New AppDomainSetup()
domaininfo.ApplicationBase = System.Environment.CurrentDirectory
Dim adevidence As Evidence = AppDomain.CurrentDomain.Evidence
SeparateAppDomain = AppDomain.CreateDomain("SeparateDomain", adevidence, domaininfo)
Dim ProxyType As Type = GetType(Proxy)
ProxyInst = DirectCast(SeparateAppDomain.CreateInstanceAndUnwrap( ProxyType.Assembly.FullName, ProxyType.FullName), Proxy)
ProxyInst.InitDomainWPFPlugin("SimpleAss", "SimpleNS.UserControl1")
Dim contract = DirectCast(SeparateAppDomain.GetData(SlotName), INativeHandleContract)
Dim control = FrameworkElementAdapters.ContractToViewAdapter(contract)
ContentHostBrDr.Child = control
...
Public Class Proxy
Inherits MarshalByRefObject
Public Sub InitDomainWPFPlugin(ByVal AssemblyName As String, ByVal TypeName As String)
Dim WPFControl As Control = DirectCast( AppDomain.CurrentDomain.CreateInstanceAndUnwrap( AssemblyName, TypeName), Control)
Dim host = New Border With {.Child = WPFControl}
Dim contract = FrameworkElementAdapters.ViewToContractAdapter(host)
AppDomain.CurrentDomain.SetData(SlotName, contract)
End Sub
End Class
What I CANT do I have been unable to get the event handling to work across the AppDomain boundary. Most examples on this require Inherits MarshalByRefObject Which a Usercontrol cant do. Examples I’ve come across about wrappers were not close enough to my scenario, or too complex for me to follow. I would be grateful for any assistance on what is required to make the events work.