需要通过WebView2获取下载进度。https://learn.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2downloadoperation?view=webview2-dotnet-1.0.865-prerelease 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;
但事件从未触发
Note(对于阅读本文的其他人):这需要版本 '1.0.865-预发布'或者稍后。首先订阅CoreWebView2.DownloadStarting
事件。您可以在CoreWebView2InitializationCompleted
事件处理程序或如上所示。
现在在CoreWebView2.DownloadStarting
您保存对事件处理程序的引用CoreWebView2DownloadOperation
并订阅BytesReceivedChanged
and 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
}
现在标签将显示进度。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)