我正在使用WebClient.DownloadFileAsync()
方法,並且想知道如何將參數傳遞給WebClient.DownloadFileCompleted
事件(或任何其他事件),並在調用的方法中使用它。將參數傳遞給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這是我原來的解決方案,但也許有些東西更優雅:)當處理幾次下載時,這變得凌亂 – mihaa123