如何在wpf中使用webclient制作下载字符串的进度条..?

发布于 2024-11-26 16:00:17 字数 888 浏览 1 评论 0原文

我制作了一个小应用程序,可以帮助下载网页的 html 内容。我制作了进度条,但使用 webclient downloadprogress Changed 事件处理程序无法获取任何值或任何更改。 这是我的代码..

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    progressBar1.Maximum = 100;
    WebClient wb = new WebClient();
    wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wb_DownloadProgressChanged);
    wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
    wb.DownloadStringAsync(new Uri("http://www.google.com"));
}

void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string htmldoc = e.Result;
}

void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

当我运行此代码时,e.progresspercentage 始终为 0,下载完成后它变为 100。所以我无法使进度条可用。有人可以告诉我这里出了什么问题吗..? 提前致谢。

I made a small application which helps to download html contents of webpages. I made progressbar and I cant get any values or any change using webclient downloadprogress changed event handler.
Here is my code..

private void Window_Loaded(object sender, RoutedEventArgs e)
{
    progressBar1.Maximum = 100;
    WebClient wb = new WebClient();
    wb.DownloadProgressChanged += new DownloadProgressChangedEventHandler(wb_DownloadProgressChanged);
    wb.DownloadStringCompleted += new DownloadStringCompletedEventHandler(wb_DownloadStringCompleted);
    wb.DownloadStringAsync(new Uri("http://www.google.com"));
}

void wb_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    string htmldoc = e.Result;
}

void wb_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
    progressBar1.Value = e.ProgressPercentage;
}

When I run this code e.progresspercentage is always 0 and when download completes it become 100. So I cant make progressbar workable. Can somebody tell me what is wrong here..?
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

坏尐絯℡ 2024-12-03 16:00:17

对此不确定,但我怀疑 DownloadXXX 方法依赖于预先报告的总大小才能报告进度。就像被动 FTP 下载不会预先报告总下载大小一样,google.com Web 服务器可能不会返回适当的标头,指示将通过管道发送的预期字节数。

Not sure about this but I suspect the DownloadXXX methods relies on the total size being reported upfront in order to report progress. Just like a passive FTP download will not report the total download size upfront, perhaps google.com web server is not returning the appropriate headers indicating the expected amount of bytes that will be sent down the pipe.

愛放△進行李 2024-12-03 16:00:17

您不能,DownloadStringAync 不会引发 DownloadProgressChanged< /a> 事件。您可以使用 DownloadDataAsync 代替,然后使用某些东西将其转换为字符串如 System.Text.Encoding,如此处所述

You can't, DownloadStringAync doesn't raise the DownloadProgressChanged event. You could use DownloadDataAsync instead, then translate it to a string, using something like System.Text.Encoding, as mentioned here.

じ违心 2024-12-03 16:00:17

根据文档,DownloadStringAsync 不报告进度。请参阅 WebClient.DownloadProgressChanged 事件 的文档。

您还想使用

using System.ComponentModel;

client.DownloadStringCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

According to the documentation, DownloadStringAsync does not report progress. See the documentation of the WebClient.DownloadProgressChanged Event.

Also you want to use

using System.ComponentModel;

client.DownloadStringCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文