CefSharp ProgressBar for indicating download progress

混江龙づ霸主 提交于 2019-12-13 00:04:31

问题


I am making a web browser based on CefSharp. When I try to download a file from the web, it prompts me to save the file, and when I press Save, it downloads the file without indicating any progress percentage. I want to add a ProgressBar to show download progress. How can I do this?


回答1:


You can use an IDownloadHandler for that.

Suppose you have a browser control named Browser and a ProgressBar named Barin your XAML, you can do this in your code behind:

Browser.DownloadHandler = new MyDownloadHandler(Bar);

Here's the code for the MyDownloadHandler:

public class MyDownloadHandler : IDownloadHandler {
    private ProgressBar _bar;

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

    public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) {

    }

    public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) {
        _bar.Dispatcher.Invoke(new Action(() => {
            Debug.Print("{0}/{1} bytes", downloadItem.ReceivedBytes, downloadItem.TotalBytes);

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


来源:https://stackoverflow.com/questions/41106707/cefsharp-progressbar-for-indicating-download-progress

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!