WebView2 下载进度
WebView2 Download progress
需要通过 WebView2 获取下载进度。 https://docs.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.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
}
我试过了:
public event EventHandler BytesReceivedChanged;
BytesReceivedChanged += webView_BytesReceivedChanged;
但事件从未触发
注意(给阅读本文的其他人):这需要版本“1.0.865-prerelease”或更高版本。首先订阅CoreWebView2.DownloadStarting
活动。您可以在 CoreWebView2InitializationCompleted
事件处理程序或上面显示的类似程序中执行此操作。
现在,在 CoreWebView2.DownloadStarting
事件处理程序中,您保存对 CoreWebView2DownloadOperation
的引用并订阅 BytesReceivedChanged
和 EstimatedEndTimeChanged
。
这里是代码(假设你的WebView2
被称为'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
}
现在标签会显示进度。
需要通过 WebView2 获取下载进度。 https://docs.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.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
}
我试过了:
public event EventHandler BytesReceivedChanged;
BytesReceivedChanged += webView_BytesReceivedChanged;
但事件从未触发
注意(给阅读本文的其他人):这需要版本“1.0.865-prerelease”或更高版本。首先订阅CoreWebView2.DownloadStarting
活动。您可以在 CoreWebView2InitializationCompleted
事件处理程序或上面显示的类似程序中执行此操作。
现在,在 CoreWebView2.DownloadStarting
事件处理程序中,您保存对 CoreWebView2DownloadOperation
的引用并订阅 BytesReceivedChanged
和 EstimatedEndTimeChanged
。
这里是代码(假设你的WebView2
被称为'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
}
现在标签会显示进度。