从 Windows 窗体运行 WPF 应用程序

发布于 2024-09-15 18:16:09 字数 1983 浏览 5 评论 0原文

我对正在尝试开发的解决方案有疑问。这是我的场景: 我有一个 VB6 应用程序,我会从此应用程序调用一些 WPF 窗口。我使用 Interop Form Library 来定义 WinForm,就像从 VB6 到 WPF 的桥梁一样。 Interop WinForm 公开了启动和关闭 wpf 应用程序的方法。

为此,我在 WPF 应用程序中定义了一个如下的帮助程序:

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 ex 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

DispatchEvent 方法管理特定事件的执行,例如打开应用程序窗口。

例如,在winform上我写了这样的语句:

 MyWpfApp.StartApplicationHelper.Start()
 Do While MyWpfApp.StartApplicationHelper.IsReady = False       
     System.Windows.Forms.Application.DoEvents()
 Loop
 MyWpfApp.StartApplicationHelper.DispatchEvent("OpenWin1")

这样我可以定义一个InteropFormMethod来通过Interop WinForm从VB6打开wpf窗口。

这个解决方案似乎有效,但我在特定用例中遇到问题,其中 wpf 应用程序停止(关闭)然后重新启动(启动)。这是显示的错误消息:“无法在同一 AppDomain 中创建多个 System.Windows.Application 实例”。

我正在尝试修改我的助手来管理这个案例,但我仍然没有找到解决方案。我会清理 AppDomain 以重新启动 wpf 应用程序。 我该怎么办?你能帮助我吗?

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 ex 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

好久不见√ 2024-09-22 18:16:10

LukePet,这是另一个相关问题: 使用 WPF在WinForms应用程序中,应用程序资源放在哪里?

也许它可以为您提供所需的所有帮助。

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.

睫毛上残留的泪 2024-09-22 18:16:10

最简单的选择就是保持 WPF 应用程序“运行”:

当您想要关闭 WPF 应用程序时,只需关闭所有打开的 WPF 窗口并执行所需的任何清理操作,但不要调用 Shutdown。

稍后,当 WPF 部分“重新启动”时,只需打开主窗口,而无需重新初始化应用程序。

您的另一个选择是在另一个 AppDomain 中运行 WPF 应用程序,并在启动/停止时加载和卸载它,这更复杂,使得 WPF 部分和应用程序的其余部分之间的通信变得困难,并且在我看来,这不值得案件。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文