dotnetbrowser 从 JavaScript 调用 .NET - ThreadProblem

dotnetbrowser Calling .NET from JavaScript - ThreadProblem

[dotnetbrowser] 从 JavaScript 调用 .NET - ThreadProblem

我正在使用 vb net,vs 2017 和 WPF,没有 mvvm。

我的 Wpf Mainwindow 包含一个 dotnetbrowser-Webbrowser 控件(仅限评估版)。看起来是这样的:

            <wpf:WPFBrowserView x:Name="WebBrowser1" Background="Transparent"  Panel.ZIndex="1110"  
                                Margin="-5,-5,0,0"   Grid.ColumnSpan="2" 
                        BrowserType="LIGHTWEIGHT"           VerticalAlignment="Top" HorizontalAlignment="Left"
                                 Width="300" Height="300" >
                <wpf:WPFBrowserView.Preferences>
                    <DotNetBrowser:BrowserPreferences TransparentBackground="True"/>
                </wpf:WPFBrowserView.Preferences>
            </wpf:WPFBrowserView>

我定义了这样一个事件:

         AddHandler WebBrowser1.Browser.ScriptContextCreated,
                    Sub(sender As Object, e As ScriptContextEventArgs)
                        Dim value As JSValue = WebBrowser1.Browser.ExecuteJavaScriptAndReturnValue("window")
                        value.AsObject().SetProperty("Account", New Account())
                    End Sub

帐户如下所示:

    Public Class Account
        Public Sub Save(firstName As String, lastName As String)
            Try
                MessageBox.Show(firstName & " " & lastName)
                Dim freileg As New winLeg(firstName)
                freileg.Show()

            Catch ex As Exception
                l(ex.ToString())
            End Try
        End class

消息框正确显示名字和姓氏。但是新的 WPFWindow(winleg) 粉碎错误:

Beim aufrufenden Thread muss es sich um einen STA-Thread handeln, da dies 
für viele Komponenten der Benutzeroberfläche erforderlich ist.

(engl: 调用线程必须是 STA,因为它对 UI 的许多组件都是必需的)

我需要在我的主 wpf-window 线程中返回数据。 我不太习惯线程(Backgroundworker 除外),所以我一无所知。任何帮助将不胜感激。

尝试使用应用程序调度程序调用您的代码。 这是 Account 实施示例:

Public Class Account
    Public Sub Save(firstName As String, lastName As String)
        Application.Current.Dispatcher.BeginInvoke(
                Sub()
                    Try
                        Dim window As New Window()
                        window.Title = firstName & " " & lastName
                        window.Width = 320
                        window.Height = 240
                        window.Show()
                    Catch ex As Exception
                        Debug.WriteLine(ex.ToString())
                    End Try
                End Sub)
    End Sub
End Class