首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在VB.net中使用"ExecuteScriptAsync“方法获取返回值?

如何在VB.net中使用"ExecuteScriptAsync“方法获取返回值?
EN

Stack Overflow用户
提问于 2019-08-04 00:33:13
回答 2查看 2.3K关注 0票数 0

我在VB.net应用程序中使用CefSharp作为浏览器,并希望从浏览器中检索返回值。

我只能在C#中找到解决方案,但我不能在VisualBasic中使其工作。

在这段代码中,我得到了以下错误:“error: Result不是Task的成员”

代码语言:javascript
复制
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:

代码语言:javascript
复制
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)
            }      
        });
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-08-04 19:41:55

我更改了你的Javascript,让它返回值。根据注释中的建议,我更改了task的声明以更正您提到的错误。

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2021-04-09 08:27:12

代码语言:javascript
复制
    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("<!--!-->", "")
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57340221

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档