我尝试为带有cefsharp的网站做一个小的下载器。对于下载文件,我使用CEFSHARP的非常好的示例下载Handler。现在,下载完成后,我想做一些事情。我知道" downloadItem "具有" iscomplete "值。但是,如果我在" ondownloadupdated "的方法中放置这样的东西,则永远不会执行内容
if (downloadItem.IsComplete) {
//do stuff
}
所以,我的问题是,我必须在哪里检查下载的物品是否完成?
编辑:完成下载手:
public class DownloadHandler : IDownloadHandler {
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
private bool stop;
private DownloadWindow window;
public void OnBeforeDownload(IBrowser browser, DownloadItem downloadItem, IBeforeDownloadCallback callback) {
var handler = OnBeforeDownloadFired;
if (handler != null) {
handler(this, downloadItem);
}
if (!callback.IsDisposed) {
using (callback) {
callback.Continue(downloadItem.SuggestedFileName, showDialog: true);
}
}
window = new DownloadWindow(downloadItem.TotalBytes, downloadItem.SuggestedFileName, this);
window.Show();
}
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback) {
var handler = OnDownloadUpdatedFired;
if (handler != null) {
handler(this, downloadItem);
}
if (window != null) {
window.UpdateProgress(downloadItem.ReceivedBytes);
}
if (stop) {
callback.Cancel();
}
}
public void StopDownload() {
stop = true;
}
}
您需要在OnDownloadUpdated
方法中检查它。
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
if (downloadItem.IsComplete || downloadItem.IsCancelled)
{
//do stuff
}
}
别忘了,您的班级必须实现IDownloadHandler
接口。一个好的调试策略将是将断点放在此方法中,以查看/了解何时被触发。