【发布时间】:2014-01-14 13:58:50
【问题描述】:
在 WPF 应用程序中使用 WebClient,以下代码可以正常工作,当下载图像时正确触发事件。
我需要将一些参数传递给ImageDownloadCompleted,以便具体知道刚刚下载了哪个图像。
使用webClient.DownloadDataAsync(new Uri(url), url); 我无法获得想要的结果。
我在这里做错了什么?
PS:基本上我会使用这个参数在一个数组中排序图像结果。如果这是实现此目的的另一种方法,请告诉我。
private void DownloadAndPrintImagesAsync(IEnumerable<string> urls)
{
foreach (var url in urls)
{
var webClient = new WebClient();
webClient.DownloadDataCompleted += ImageDownloadCompleted;
webClient.DownloadDataAsync(new Uri(url), url); // I want to pass url
}
}
private void ImageDownloadCompleted(object sender, DownloadDataCompletedEventArgs e)
{
if (!e.Cancelled && e.Error == null)
{
// I need to get url here
}
}
【问题讨论】: