使用 CefSharp 中的“ProgressBar”页面加载和更新进行可视化

Visualize using the "ProgressBar" page loading and updating in CefSharp

提问人:eusataf 提问时间:7/13/2022 最后编辑:eusataf 更新时间:7/15/2022 访问量:359

问:

使用页面加载和更新进行可视化。
我试着去做,但没有任何效果。
ProgressBarCefSharp

在方法中设置类
中的断点:
DownloadHandlerSefSharp.cs

  • CanDownload;
  • OnBeforeDownload;
  • OnDownloadUpdated.

但是在调试时,那里没有任何反应。

enter image description here

enter image description here

enter image description here

frmProgressBar.cs

using CefSharp;
using CefSharp.WinForms;

namespace CefSharpWinFormQuestion.ProgressBarQS.f01Create
{
    public partial class frmProgressBar : Form
    {        
        public frmProgressBar()
        {
            InitializeComponent();
            InitializeChronium();

            textBox1.Text = @"https://github.com/";
        }

        public void InitializeChronium()
        {
            Cef.Initialize(new CefSettings());
            chromiumWebBrowser1.DownloadHandler = new DownloadHandlerSefSharp(progressBar1);
        }

        private void frmProgressBar_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;

            chromiumWebBrowser1.LoadUrlAsync(url);
        }
    }
} 

下载处理程序SefSharp.cs

using System.Windows.Forms;
using CefSharp;


namespace CefSharpWinFormQuestion.ProgressBarQS.f01Create
{
    public class DownloadHandlerSefSharp : IDownloadHandler
    {
        private ProgressBar _bar;
        

        public DownloadHandlerSefSharp(ProgressBar bar)
        {
            _bar = bar;
        }

        public bool CanDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, string url, string requestMethod)
        {
             throw new NotImplementedException();
        }

        void IDownloadHandler.OnBeforeDownload(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback)
        {
            // throw new NotImplementedException();
        }

        void IDownloadHandler.OnDownloadUpdated(IWebBrowser chromiumWebBrowser, IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
        {
            // _bar.dis

            _bar.Invoke(new Action(() => {
                Debug.Print("{0}/{1} bytes", downloadItem.ReceivedBytes, downloadItem.TotalBytes);

                _bar.Maximum = (int)downloadItem.TotalBytes;
                _bar.Value = (int)downloadItem.ReceivedBytes;
            }));
        }
    }
}

更新-1

frmProgressBar.cs

public partial class frmProgressBar : Form
    {    

        public frmProgressBar()
        {
            InitializeComponent();
            InitializeChronium();

            textBox1.Text = @"https://github.com/";
        }

        public void InitializeChronium()
        {
            Cef.Initialize(new CefSettings());
            chromiumWebBrowser1.DisplayHandler = new DownloadHandlerSefSharp(progressBar1);
        }
      

        private void frmProgressBar_FormClosing(object sender, FormClosingEventArgs e)
        {
            Cef.Shutdown();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string url = textBox1.Text;

            chromiumWebBrowser1.LoadUrlAsync(url);
        }
    }

下载处理程序SefSharp.cs

  public class DownloadHandlerSefSharp : DisplayHandler
    {
        private ProgressBar _bar;


        public DownloadHandlerSefSharp(ProgressBar bar)
        {
            _bar = bar;
        }

        protected override void OnLoadingProgressChange(
                    IWebBrowser chromiumWebBrowser, 
                    IBrowser browser,
                    double progress)
        {
            _bar.Invoke(new Action(() => {                               

                _bar.Value = (int)progress;
            }));
        }
    }
C# Winforms Cefsharp

评论


答:

2赞 amaitland 7/14/2022 #1

IDownloadHandler 将提供文件下载通知,而不是页面加载通知。您的示例加载 GitHub,导航到存储库并下载其源代码,然后应调用您的方法。

对于页面加载进度通知,请使用 IDisplayHandler.OnLoadingProgressChange

评论

0赞 eusataf 7/14/2022
我不太明白。你能展示一个代码的例子吗?
0赞 amaitland 7/15/2022
你有什么不明白的?创建一个继承自 CefSharp.Handler.DisplayHandler 的类并重写 DisplayHandler.OnLoadingProgressChange 方法,省去实现所有方法。与下载处理程序的概念完全相同。
0赞 eusataf 7/15/2022
请参阅 Update-1。已开始显示指示。问题: 1.我做对了吗?阿拉伯数字。如何确定 的最大值 ?ProgressBarProgressBar
0赞 amaitland 7/15/2022
根据文档,progess 范围从 0.0 到 1.0。因此,如果您需要百分比,请将该值乘以 100。