Run WPF Application from a Windows Form
I have a problem with a solution that I'm trying to develope. This is my scenario: I have a VB6 application and I would call from this application some WPF windows. I've used the Interop Form Library to define a WinForm like a bridge from VB6 to WPF. The Interop WinForm exposes the methods to start and shutdown the wpf application.
To do that, in the WPF Application I've defined an helper like this:
Public Class StartApplicationHelper
Public Shared Property IsReady As Boolean = False
Public Shared Event NotifyEvent As ValueEnterEventHandler
Public Shared Sub Start()
If System.Windows.Application.Current Is Nothing Then
Try
Dim myApp As Application = New Application
myApp.ShutdownMode = ShutdownMode.OnExplicitShutdown
myApp.InitializeComponent()
IsReady= True
Catch e开发者_如何学Cx As Exception
MessageBox.Show(ex.Message)
End Try
End If
End Sub
Public Shared Sub Shutdown()
If System.Windows.Application.Current IsNot Nothing Then
System.Windows.Application.Current.Shutdown()
IsReady = False
End If
End Sub
Public Shared Sub DispatchEvent(ByVal eve As String)
If IsReady Then EventDispatcherService.DispatchEvent(eve, New EventDispatcherDataChildWin(String.Empty, Nothing, Nothing, False))
End Sub
Public Shared Sub DispatchResult(ByVal res As Object)
RaiseEvent NotifyEvent(Nothing, New ValueEnterEventArgs(res))
End Sub
End Class
the method DispatchEvent manage the execute of specific event like the opening of an application window.
For example, on winform I've wrote this statements:
MyWpfApp.StartApplicationHelper.Start()
Do While MyWpfApp.StartApplicationHelper.IsReady = False
System.Windows.Forms.Application.DoEvents()
Loop
MyWpfApp.StartApplicationHelper.DispatchEvent("OpenWin1")
in this way I can define an InteropFormMethod to open wpf window from VB6 across the Interop WinForm.
This solution seems work but I have a problem with a particular use case where the wpf application are stopped (shutdown) and then restarted (start). This is the displayed error message: "Cannot create more than one System.Windows.Application instance in the same AppDomain".
I'm trying to modify my helper to manage this case but I still have not found a solution. I would clean the AppDomain to restart wpf application. How can I do? Can you help me?
LukePet, here is another related question: WPF used within a WinForms application, where to put Application resources?
Perhaps it gives you all the help you need.
The easiest option is just to keep the WPF application "running":
When you want to close the WPF app just close all the open WPF window and do any cleanup you want but don't call Shutdown.
Later, when the WPF part is "restarted" just open the main window without reinitializing the application.
Your other option is running the WPF app in another AppDomain and loading and unloading it when starting/stopping, this is more complicated, makes communication between the WPF part and the rest of the app difficult and, in my opinion, not worth it in this case.
精彩评论