CEF敏锐的浏览器等待网站完全加载 [英] CEF sharp browser wait till website fully loaded

查看:482
本文介绍了CEF敏锐的浏览器等待网站完全加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CEFsharp浏览器,并通过LoadingStateChanged事件确定页面是否完成加载,但是它会触发很多次。

I am using CEFsharp browser and determine the page finish loading with LoadingStateChanged event but it fires many times.

仅在页面完全加载后才需要触发它,怎么办?

I need it to fire only once the page has fully loaded, how can this be done?

private async void Browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
{
    if (!e.Browser.IsLoading)
    {
        await Task.Run(async () =>
        {
            await Task.Delay(3000);
        });

        try
        {
            MessageBox.Show("Page has been loaded");
        }
        catch (Exception ex)
        {

        }
    }
}


推荐答案

对于那些感兴趣的人,我使用以下变通方法。
我会跟踪已发出的请求的数量,并且仅在有新请求时才执行操作。
就我而言,这似乎很好。但是我可以想象,浏览器实际上仍然可以执行某些操作,有时您需要等待最后一次完成。

For those interested, I use (a variation of) the following workaround. I keep track of the number of requests made and only do an action if there is a newer request. For my case, this seems to work fine. However I can imagine that the browser actually still does something and you would sometimes need to wait for the last time it finishes.

using CefSharp;

public class RequestHandler : IRequestHandler
{
    //left all the irrelevant IRequestHandler methods out of this example code, but you'll need to implement them
    public int NrOfCalls { get; set; }
    public bool OnBeforeBrowse(IWebBrowser chromiumWebBrowser, IBrowser browser, IFrame frame, IRequest request, bool userGesture, bool isRedirect)
    {
        NrOfCalls++;
        return false;
    }
}
public class Handle
{
    private RequestHandler _requestHandler;
    private IWebBrowser _browser;
    private int previousRequestNrWhereLoadingFinished = -1;

    public Handle()
    {
        _requestHandler = new RequestHandler();
        _browser.RequestHandler = _requestHandler;
    }
    private void _browser_LoadingStateChanged(object sender, LoadingStateChangedEventArgs e)
    {
        // Check if page has finished loading
        if (!e.IsLoading)
        {
            //sometimes this is called multiple times for one request, we will only do something if it comes from a newer request than the previous action
            if (previousRequestNrWhereLoadingFinished < _requestHandler.NrOfCalls)
            {
                previousRequestNrWhereLoadingFinished = _requestHandler.NrOfCalls;
                ThisMethodWillOnlyBeCalledOncePerRequest();
            }
        }
    }
}

这篇关于CEF敏锐的浏览器等待网站完全加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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