How to hide the Download dialog in WebView2 while downloading page content or printing with "Save as PDF" option?

Viewed 50

I know how to subscribe to the CoreWebview2.DownloadStarting event and use handled = true to stop the Download dialog from showing while a download (i.e. an image) is being made, but the problem is the DownloadStarting event never fires if you right click on a web page and choose "Save as" or "Print > Save as PDF", even though the Download dialog will appear as if a regular download was being made. Does anyone know any workaround for this?

My code:

public Form1()
{
    InitializeComponent();
}

private async void button1_Click(object sender, EventArgs e)
{
    if (webView != null)
        webView.Dispose();

    webView = new WebView2();
    await webView.EnsureCoreWebView2Async();
    webView.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
    panel1.Controls.Add(webView);
    webView.Dock = DockStyle.Fill;

    webView.Source = new Uri("https://www.microsoft.com");
}

private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
      e.Handled = true;
}
1 Answers

I just found a solution:

  1. subscribe to CoreWebView2.IsDefaultDownloadDialogOpenChanged event:
webView.CoreWebView2.IsDefaultDownloadDialogOpenChanged += webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged;
  1. Close the Download dialog if it's open:
private void webView_CoreWebView2_IsDefaultDownloadDialogOpenChanged(object sender, object e)
{
   if (webView.CoreWebView2.IsDefaultDownloadDialogOpen) webView.CoreWebView2.CloseDefaultDownloadDialog();
}
Related