问题
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 Bar
in 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