I'm including the following because the implementation of OnBeforeDownloadFired() isn't shown in many online examples of how to use the DownloadHandler class, and it's missing from the DownloadHandler.cs cited.
This helped solve a nagging issue with downloading files (eg .mobi ebook) if the download link had the target "_blank". If there was no target, a download dialog was triggered. With a _blank target, I had to suppress a popup window and open a new custom tab in my browser, but when this happened, a download dialog was not triggered.
I think this is right. Hope it helps, or at least gives you a start:
DownloadHandler downer = new DownloadHandler(this);
browser.DownloadHandler = downer;
downer.OnBeforeDownloadFired += OnBeforeDownloadFired;
downer.OnDownloadUpdatedFired += OnDownloadUpdatedFired;
private void OnBeforeDownloadFired(object sender, DownloadItem e)
{
this.UpdateDownloadAction("OnBeforeDownload", e);
}
private void OnDownloadUpdatedFired(object sender, DownloadItem e)
{
this.UpdateDownloadAction("OnDownloadUpdated", e);
}
private void UpdateDownloadAction(string downloadAction, DownloadItem downloadItem)
{
/*
this.Dispatcher.Invoke(() =>
{
var viewModel = (BrowserTabViewModel)this.DataContext;
viewModel.LastDownloadAction = downloadAction;
viewModel.DownloadItem = downloadItem;
});
*/
}
// ...
public class DownloadHandler : IDownloadHandler
{
public event EventHandler<DownloadItem> OnBeforeDownloadFired;
public event EventHandler<DownloadItem> OnDownloadUpdatedFired;
MainForm mainForm;
public DownloadHandler(MainForm form)
{
mainForm = form;
}
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);
}
}
}
public void OnDownloadUpdated(IBrowser browser, DownloadItem downloadItem, IDownloadItemCallback callback)
{
var handler = OnDownloadUpdatedFired;
if (handler != null)
{
handler(this, downloadItem);
}
}
}
// ...