如何在VB.net中使用“ ExecuteScriptAsync”方法检索返回的值?

时间:2019-08-03 16:33:12

标签: vb.net cefsharp c#-to-vb.net

我正在将CefSharp用作VB.net应用程序中的浏览器,我想从浏览器中获取返回值。

我只能在C#中找到解决方案,但无法在VisualBasic中使用它。

在此代码中,我收到以下错误:'错误:结果不是Task的成员'

<!-- added a "data-wait-until" attribute to the image that you can use to change the delay time before translating takes place per yor requirement -->

<!-- if you ommit it 1 second delay is used instead -->

<!-- in this demo I applied a 1 second delay by using data-wait-until="1000" (1000 ms = 1s) -->

<p class="info">hover/click the next image and see what happens</p>
<img src="https://via.placeholder.com/250" id="spirit" title="hover or click me !" data-wait-until="1000" />

这是我在CefSharp文档中找到的C#版本,但是我无法将其转换为VB.net:

.html()

2 个答案:

答案 0 :(得分:1)

我更改了Javascript,以便它返回一个值。如评论中所建议,我更改了task的声明以更正您提到的错误。

Dim script = "(function(){ var value; value=10-2; return value; })();"
Dim task As Task(Of JavascriptResponse) = browser.EvaluateScriptAsync(script)
Dim taskResult As String

task.ContinueWith(
    Sub(t)
        If t.IsFaulted = False Then
            Dim response = t.Result 'Error: Result is not a member of Task' 
            If response.Success And response.Result IsNot Nothing Then
                taskResult = response.Result
            End If
        End If
    End Sub)

MsgBox(task.Result.Result)

答案 1 :(得分:-1)

    Dim html As String = ""
    Using t As Task(Of String) = WebView21.ExecuteScriptAsync("document.documentElement.outerHTML;")
        Do Until t.IsCompleted
            Application.DoEvents()
        Loop
        html = t.Result
    End Using
    html = Regex.Unescape(html)
    html = html.Remove(0, 1)
    html = html.Remove(html.Length - 1, 1)
    html = html.Replace("<!--!-->", "")