【发布时间】:2017-01-05 09:49:00
【问题描述】:
我正在使用WebClient.DownloadFileAsync() 方法,并且想知道如何将参数传递给WebClient.DownloadFileCompleted 事件(或与此相关的任何其他事件),并在调用的方法中使用它。
我的代码:
public class MyClass
{
string downloadPath = "some_path";
void DownloadFile()
{
int fileNameID = 10;
WebClient webClient = new WebClient();
webClient.DownloadFileCompleted += DoSomethingOnFinish;
Uri uri = new Uri(downloadPath + "\" + fileNameID );
webClient.DownloadFileAsync(uri,ApplicationSettings.GetBaseFilesPath +"\" + fileNameID);
}
void DoSomethingOnFinish(object sender, AsyncCompletedEventArgs e)
{
//How can i use fileNameID's value here?
}
}
如何将参数传递给DoSomethingOnFinish()?
【问题讨论】:
-
我现在能想到的唯一方法是,您可以将文件名保存在全局私有字段中并在
DoSomethingOnFinish中访问它 -
@ChristophKn 那是我最初的解决方案,但我想也许有更优雅的东西:) 当处理多次下载时,这会变得很混乱
标签: c# asynchronous unity3d unity5