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

How can i retrieve a returned value with the "ExecuteScriptAsync" method in VB.net?

我在 VB.net 应用程序中使用 CefSharp 作为浏览器,我想从浏览器中检索 return 值。

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

在此代码中出现以下错误:'Error: Result is not a member of Task'


Dim script = "var returnValue = function(){ var value; value=10-2; return value; }"
        Dim task As Threading.Tasks.Task = 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(taskResult)

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

browser.EvaluateScriptAsync(script).ContinueWith(x =>
        {
            var response = x.Result;

            if (response.Success && response.Result != null)
            {
                var onePlusOne = (int)response.Result;
                //Do something here (To interact with the UI you must call BeginInvoke)
            }      
        });

我更改了您的 Javascript 使其成为 returns 值。根据评论中的建议,我更改了 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)

    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("<!--!-->", "")