CefSharp Winfoms页面加载并等待 [英] CefSharp winfoms Page load and wait

查看:2129
本文介绍了CefSharp Winfoms页面加载并等待的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究应用程序中的 Cefsharp Offscreen 。加载页面的文档中提供的代码为:

I'm working on Cefsharp Offscreen in my application. The code provided in documentation to load page is:

 const string testUrl = "https://github.com/cefsharp/CefSharp/wiki/Quick-Start";
  var settings = new CefSettings()
        {
   //By default CefSharp will use an in-memory cache, you need to specify a Cache Folder to persist data
   CachePath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "CefSharp\\Cache")
        };

   //Perform dependency check to make sure all relevant resources are in our output directory.
   Cef.Initialize(settings, performDependencyCheck: true, browserProcessHandler: null);

   // Create the offscreen Chromium browser.
   browser = new ChromiumWebBrowser(testUrl);

   // An event that is fired when the first page is finished loading.
   // This returns to us from another thread.
   browser.LoadingStateChanged += BrowserLoadingStateChanged;
   Cef.Shutdown();

但是我想要这样的东西

browser = new ChromiumWebBrowser(); //without testUrl
 // and then 
browser.Load(testUrl).Wait();// and wait something like that;
// load url and wait unit page is fully loaded then go to next line

I找不到任何解决方案。

I couldn't find any solution.

推荐答案

CefSharp.OffScreen.Example.Program.cs 类包含使用< a href = https://docs.microsoft.com/zh-cn/dotnet/api/system.threading.tasks.taskcompletionsource-1?view=netframework-4.8 rel = nofollow noreferrer> TaskCompletionSource 包装 LoadingStateChanged 事件,以便可以 await 页面加载

The CefSharp.OffScreen.Example.Program.cs class in the project source contains an example of using a TaskCompletionSource to wrap the LoadingStateChanged event so you can await the Page Load.

实现为扩展方法的基本示例如下:

A basic example implemented as an extension method would look like:

public static class WebBrowserExtensions
{
    public static Task LoadPageAsync(this IWebBrowser browser, string address = null)
    {
        var tcs = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);

        EventHandler<LoadingStateChangedEventArgs> handler = null;
        handler = (sender, args) =>
        {
            //Wait for while page to finish loading not just the first frame
            if (!args.IsLoading)
            {
                browser.LoadingStateChanged -= handler;
                //Important that the continuation runs async using TaskCreationOptions.RunContinuationsAsynchronously
                tcs.TrySetResult(true);
            }
        };

        browser.LoadingStateChanged += handler;

        if (!string.IsNullOrEmpty(address))
        {
            browser.Load(address);
        }

        return tcs.Task;
    }
}

然后您可以编写类似

browser = new ChromiumWebBrowser();
await browser.LoadPageAsync(testUrl);

将浏览器编写为 async 不建议也不支持使用 Task.Wait 进行阻止。使用 async / await

The browser is written to be async and blocking using Task.Wait is not recommended nor supported. Use async/await.

这篇关于CefSharp Winfoms页面加载并等待的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆