在按钮事件函数中,我有一个下载多个文件的多重链接。要下载这些文件,我有以下代码:
for (int l = 0; l < itemLinks.Count(); l++)
{
string[] sourceParts = itemLinks[l].Split('/');
string fileName = sourceParts[sourceParts.Count() - 1];
WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadCompleted += client_OpenReadCompleted;
client.OpenReadAsync(new Uri(itemLinks[l]));
}
在 OpenReadCompletedEventArgs 的以下函数中,我需要知道哪个文件的下载完成:
async void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
string pff = e.ToString();
byte[] buffer = new byte[e.Result.Length];
await e.Result.ReadAsync(buffer, 0, buffer.Length);
using (IsolatedStorageFile storageFile =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = storageFile
.OpenFile(FILE_NAME, FileMode.Create))
{
await stream.WriteAsync(buffer, 0, buffer.Length);
}
}
//Also I need to do some stuff here with FILE_NAME
}
如何将 FILE_NAME 值发送到 client_OpenReadCompleted?
我不能将值保存在全局变量中,因为它会在 for 语句的每次调用中发生变化,我也尝试将变量发送为 += (sender, eventArgs) => 但是我的代码中有 await 这迫使我将按钮功能更改为异步