WebView2 下载进度 [英] WebView2 Download progress

查看:116
本文介绍了WebView2 下载进度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

需要通过 WebView2 获取下载进度.https://docs.microsoft.com/en-us/dotnet/api/microsoft.web.webview2.core.corewebview2downloadoperation?view=webview2-dotnet-1.0.865-prerelease

Need to get download progress via 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\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
}

我试过了:

public event EventHandler BytesReceivedChanged;
BytesReceivedChanged += webView_BytesReceivedChanged;

但事件从未触发

推荐答案

注意(致阅读本文的其他人):这需要版本1.0.865-prerelease"或之后.首先订阅 CoreWebView2.DownloadStarting 事件.您可以在 CoreWebView2InitializationCompleted 事件处理程序中执行此操作,或者如上所示.

Note (to others reading this): This requires version '1.0.865-prerelease' or later. First subscribe to the CoreWebView2.DownloadStartingevent. You can do that in the CoreWebView2InitializationCompletedeventhandler or like shown above.

现在在 CoreWebView2.DownloadStarting 事件处理程序中保存对 CoreWebView2DownloadOperation 的引用并订阅 BytesReceivedChangedEstimatedEndTimeChanged.

Now in the CoreWebView2.DownloadStarting eventhandler you save a reference to the CoreWebView2DownloadOperationand subscribe to the BytesReceivedChanged and EstimatedEndTimeChanged.

这是代码(假设您的 WebView2 被称为webView21"):

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
}

现在标签将显示进度.

这篇关于WebView2 下载进度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆