CefSharp 库中是否有事件 WebBrowser.Navigating 的模拟?

Is there analog to event WebBrowser.Navigating in CefSharp library?

我需要在单击站点上的下载按钮并立即开始下载时调用事件 link。我知道它如何与 WebBrowser 一起工作:

private void WebBrowser_Navigating(object sender, NavigatingCancelEventArgs e)
{
    string url = e.Uri.ToString();
    if (url.Contains("https://accounts.google.com/AccountChooser") 
    {
        _userAgentController.UserAgentRefresh();
    }

    if (url.Contains("get:")) //specific form of download link
    {
        DownloadModel(url);
    }
}

但我应该在 CefSharp 库中使用它并使用下载 url.

的 DownloadModel() 方法

我尝试了 LoadingChanged 和 FrameLoadEnd 事件,但无法获得所需的 link。

看起来 CefSharp 提供了一个接口供您实现,该接口具有下载回调。

这是一个例子:

  public class DownloadHandler : IDownloadHandler
    {
        public event EventHandler<DownloadItem> OnBeforeDownloadFired;

        public event EventHandler<DownloadItem> OnDownloadUpdatedFired;

        public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            var handler = OnBeforeDownloadFired;
            if (handler != null)
            {
                handler(this, downloadItem);
            }

            if (!callback.IsDisposed)
            {
                using (callback)
                {
                    callback.Continue(downloadItem.SuggestedFileName, showDialog: true);
                }
            }
        }

        public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            var handler = OnDownloadUpdatedFired;
            if (handler != null)
            {
                handler(this, downloadItem);
            }
        }
    }

从这里开始,下载完成后,您将获得 DownloadItem 对象,它提供了您需要的完整路径:

请在此处查看 class 结构:https://github.com/cefsharp/CefSharp/blob/0a2693fa9ba7273ada5df363bf78e85b5a1a342b/CefSharp/DownloadItem.cs