我尝试使用CefSharp为网站制作一个小型下载工具。对于下载文件,我使用CefSharp中非常好的示例DownloadHandler。现在,我想在下载完成后做一些事情。我知道“ downloadItem ”具有“ IsComplete ”值。但是如果我在“ OnDownloadUpdated ”方法中添加这样的东西,那么东西永远不会被执行
if (downloadItem.IsComplete) {
//do stuff
}
所以,我的问题是,我必须检查下载的项目是否已完成?
编辑:完成DownloadHandler:
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;
}
}
答案 0 :(得分:2)
您需要在OnDownloadUpdated
方法中进行检查。
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
if (downloadItem.IsComplete || downloadItem.IsCancelled)
{
//do stuff
}
}
别忘了,您的班级必须实施IDownloadHandler
界面。
一个好的调试策略是在这个方法中放置断点,以查看/理解何时被触发。