Home > Enterprise >  Visualize using the "ProgressBar" page loading and updating in CefSharp
Visualize using the "ProgressBar" page loading and updating in CefSharp

Time:07-16

Visualize using the ProgressBar page loading and updating in CefSharp.
I try to do it, but nothing works.

Set breakpoints in the DownloadHandlerSefSharp.cs class
in the methods:

  • CanDownload;
  • OnBeforeDownload;
  • OnDownloadUpdated.

But when debugging, nothing happens there.

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);
        }
    }
} 

DownloadHandlerSefSharp.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;
            }));
        }
    }
}

Update-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);
        }
    }

DownloadHandlerSefSharp.cs

  public class DownloadHandlerSefSharp : DisplayHandler
    {
        private ProgressBar _bar;


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

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

                _bar.Value = (int)progress;
            }));
        }
    }

CodePudding user response:

IDownloadHandler will provide notifications for downloading of files, not page load notifications. Your example loads GitHub, navigate to a repository and download it's source, your methods should then be called.

For page load progress notifications use IDisplayHandler.OnLoadingProgressChange

  • Related