Partilhar via


CoreWebView2.DownloadStarting Event

Definition

DownloadStarting is raised when a download has begun, blocking the default download dialog, but not blocking the progress of the download.

public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2DownloadStartingEventArgs> DownloadStarting;
member this.DownloadStarting : EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2DownloadStartingEventArgs> 
Public Custom Event DownloadStarting As EventHandler(Of CoreWebView2DownloadStartingEventArgs) 

Event Type

Examples

_iWebView2.CoreWebView2.DownloadStarting += delegate (
  object sender, CoreWebView2DownloadStartingEventArgs args)
{
    // Developer can obtain a deferral for the event so that the CoreWebView2
    // doesn't examine the properties we set on the event args until
    // after the deferral completes asynchronously.
    CoreWebView2Deferral deferral = args.GetDeferral();

    // We avoid potential reentrancy from running a message loop in the download
    // starting event handler by showing our download dialog later when we
    // complete the deferral asynchronously.
    System.Threading.SynchronizationContext.Current.Post((_) =>
    {
        using (deferral)
        {
            // Hide the default download dialog.
            args.Handled = true;
            var dialog = new TextInputDialog(
                title: "Download Starting",
                description: "Enter new result file path or select OK to keep default path. Select cancel to cancel the download.",
                defaultInput: args.ResultFilePath);
            if (dialog.ShowDialog() == true)
            {
                args.ResultFilePath = dialog.Input.Text;
                UpdateProgress(args.DownloadOperation);
            }
            else
            {
                args.Cancel = true;
            }
        }
    }, null);
};
_iWebView2.CoreWebView2.Navigate("https://demo.smartscreen.msft.net/");

Remarks

The host can choose to cancel a download, change the result file path, and hide the default download dialog. If download is not handled or canceled, the download is saved to the default path after the event completes with default download dialog shown.

Applies to