如何在wpf中使用webclient制作下载字符串的进度条..?
我制作了一个小应用程序,可以帮助下载网页的 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
对此不确定,但我怀疑 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.
您不能,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.
根据文档,DownloadStringAsync 不报告进度。请参阅 WebClient.DownloadProgressChanged 事件 的文档。
您还想使用
According to the documentation, DownloadStringAsync does not report progress. See the documentation of the WebClient.DownloadProgressChanged Event.
Also you want to use