[英]dotnetbrowser Calling .NET from JavaScript - ThreadProblem
[dotnetbrowser] Calling .NET from JavaScript - ThreadProblem [dotnetbrowser]从JavaScript调用.NET-ThreadProblem
i am using vb net , vs 2017 and WPF, no mvvm. 我正在使用vb net,vs 2017和WPF,没有mvvm。
My Wpf Mainwindow contains a dotnetbrowser-Webbrowser control (evaluation only). 我的Wpf Mainwindow包含一个dotnetbrowser-Webbrowser控件(仅评估)。 Looks this way: 看起来是这样的:
<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>
I defined an event like this: 我定义了这样的事件:
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
The account looks like this: 该帐户如下所示:
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
The messagebox shows firstname lastname correctly. 该消息框正确显示了姓氏。 But the new WPFWindow(winleg) smashes error: 但是新的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: the calling thread must be STA cause its nessesary for many components of the UI) (engl:调用线程必须是STA,导致它对于UI的许多组件都是必需的)
I need the returned data within my main wpf-window thread. 我需要在主wpf窗口线程中返回数据。 I am not really used to Threading (except Backgroundworker) so i am clueless. 我不是很习惯线程(Backgroundworker除外),所以我一无所知。 Any help would be appreciated. 任何帮助,将不胜感激。
Try to invoke your code using the application dispatcher. 尝试使用应用程序分派器调用代码。 Here is the sample Account
implementation: 这是示例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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.