Reputation: 139
Need to get download progress via WebView2. https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2downloadoperation?view=webview2-dotnet-1.0.865-prerelease
async void InitializeAsync()
{
var env = await CoreWebView2Environment.CreateAsync(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + @"\Microsoft\EdgeCore\92.0.887.0");
await webView.EnsureCoreWebView2Async(env);
webView.CoreWebView2.DownloadStarting += webView_DownloadStarting;
webView.CoreWebView2.Navigate("downloadURL");
}
private void webView_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
e.ResultFilePath = filename;
public event EventHandler<object> BytesReceivedChanged; <-- Need this event for print bytes to webView_BytesReceivedChanged
public event EventHandler<object> StateChanged; <-- Need this event for print download state to webView_StateChanged
}
private void webView_BytesReceivedChanged(object sender, CoreWebView2DownloadStartingEventArgs e)
{
Console.WriteLine(e.DownloadOperation.BytesReceived); // Bytes received
Console.WriteLine(e.DownloadOperation.TotalBytesToReceive); // Total bytes to receive
}
private void webView_StateChanged(object sender, CoreWebView2DownloadStartingEventArgs e)
{
Console.WriteLine(e.DownloadOperation.State); //2 Completed, 1 In progress, 0 Error
}
I tried:
public event EventHandler BytesReceivedChanged;
BytesReceivedChanged += webView_BytesReceivedChanged;
but event never fired
Upvotes: 4
Views: 7088
Reputation: 10930
Note (to others reading this): This requires version '1.0.865-prerelease' or later. First subscribe to the CoreWebView2.DownloadStarting
event. You can do that in the CoreWebView2InitializationCompleted
eventhandler or like shown above.
Now in the CoreWebView2.DownloadStarting
eventhandler you save a reference to the CoreWebView2DownloadOperation
and subscribe to the BytesReceivedChanged
and EstimatedEndTimeChanged
.
Here is the code (assuming your WebView2
is called 'webView21'):
using Microsoft.Web.WebView2.Core;
CoreWebView2DownloadOperation downloadOperation;
private void WebView21_CoreWebView2InitializationCompleted(object sender, CoreWebView2InitializationCompletedEventArgs e)
{
webView21.CoreWebView2.DownloadStarting += CoreWebView2_DownloadStarting;
}
private void CoreWebView2_DownloadStarting(object sender, CoreWebView2DownloadStartingEventArgs e)
{
downloadOperation = e.DownloadOperation; // Store the 'DownloadOperation' for later use in events
downloadOperation.BytesReceivedChanged += DownloadOperation_BytesReceivedChanged; // Subscribe to BytesReceivedChanged event
downloadOperation.EstimatedEndTimeChanged += DownloadOperation_EstimatedEndTimeChanged; // Subsribe to EstimatedEndTimeChanged event
}
private void DownloadOperation_EstimatedEndTimeChanged(object sender, object e)
{
label1.Text = downloadOperation.EstimatedEndTime.ToString(); // Show the progress
}
private void DownloadOperation_BytesReceivedChanged(object sender, object e)
{
label2.Text = downloadOperation.BytesReceived.ToString(); // Show the progress
}
Now the labels will show the progress.
Upvotes: 11