2016-12-12 89 views
0

我正在製作一個基於CefSharp的網頁瀏覽器。當我嘗試從網上下載文件時,它會提示我保存該文件,而當我按保存時,它會下載文件而不指示任何進度百分比。我想添加一個ProgressBar來顯示下載進度。我怎樣才能做到這一點?CefSharp ProgressBar用於指示下載進度

+0

https://github.com/cefsharp/CefSharp/commit/b47731de5ec09130a83c11e0d0776e9c245e95b7 – amaitland

回答

1

您可以使用IDownloadHandler

假設你有一個名爲Browser瀏覽器控件和一個名爲XAML中BarProgressBar,你可以在你的代碼做到這一點的背後:

Browser.DownloadHandler = new MyDownloadHandler(Bar); 

下面是該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; 
     })); 
    } 
} 
+0

非常感謝! –