0

我正在使用 Cefsharp Winforms,最近我一直在尝试切换到 Offscreen。一切正常,除了现在我的代码在返回页面源之前不等待 EvaluateScriptAsync 完成。

或者也许我只是不太明白这个任务是如何工作的。这是我到目前为止的进展:

private static void WebBrowserFrameLoadEnded(object sender, FrameLoadEndEventArgs e)
{
    var browser = (CefSharp.OffScreen.ChromiumWebBrowser)sender;

    if (e.Frame.IsMain)
    {
        browser.FrameLoadEnd -= WebBrowserFrameLoadEnded;

        var x = browser.EvaluateScriptAsync("/* some javascript codes */");

        if (x.IsCompleted && x.Result.Success)
        {
            x.ContinueWith(a =>
            {
                var task = browser.GetSourceAsync();

                task.ContinueWith(d =>
                {
                    if (d.IsCompleted)
                    {
                        globalRtnVal = d.Result;
                    }
                }).ConfigureAwait(false);
            });
        }
    }
}

我的主要代码是这样的:

/* some codes */

CefSharp.OffScreen.ChromiumWebBrowser asd = new CefSharp.OffScreen.ChromiumWebBrowser(/* url */);
asd.BrowserSettings.Javascript = CefSharp.CefState.Enabled;
asd.BrowserSettings.WebSecurity = CefSharp.CefState.Disabled;

asd.FrameLoadEnd += WebBrowserFrameLoadEnded;

int tryCount = 0;

do
{
    Thread.Sleep(3000);

    RtnHtml = globalRtnVal;

    if (String.IsNullOrEmpty(RtnHtml))
        tryCount++;

    if (tryCount == 10 && String.IsNullOrEmpty(RtnHtml))
    {
        /* some codes */
        return null;
    }
}
while (String.IsNullOrEmpty(RtnHtml));

/* some codes */
4

0 回答 0