C# 如何访问事件中的其他类
我试图在文件下载完成时触发的事件中访问下面代码块中的脚本类。我怎么能做到呢C# 如何访问事件中的其他类,c#,.net,C#,.net,我试图在文件下载完成时触发的事件中访问下面代码块中的脚本类。我怎么能做到呢 public void DownloadScript(Script script, string DownloadLocation) { AddLog(GenerateLog("Downloading Script", "Started", "Downloading " + script.Name + " from " + script.DownloadURL + "."));
public void DownloadScript(Script script, string DownloadLocation)
{
AddLog(GenerateLog("Downloading Script", "Started", "Downloading " + script.Name + " from " + script.DownloadURL + "."));
WebClient Client = new WebClient();
Client.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(Client_DownloadFileCompleted);
Client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(Client_DownloadProgressChanged);
Client.DownloadFileAsync(new Uri(script.DownloadURL), DownloadLocation + "test1.zip");
}
下面是触发的事件
public void Client_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Error.Message != string.Empty)
{
AddLog(GenerateLog("Downloading Script", "Error", "There was an error downloading " + script.Name + " from " + script.DownloadURL + ". " + e.Error.Message));
Console.WriteLine("Error");
}
else
{
AddLog(GenerateLog("Downloading Script", "Done", "Finished downloading " + script.Name + " from " + script.DownloadURL + "."));
Console.WriteLine("Done");
}
}
您可以使用lambda表达式捕获
脚本
对象,并将其作为额外参数传递给处理程序
public void DownloadScript(Script script, string DownloadLocation) {
...
WebClient Client = new WebClient();
Client.DownloadFileCompleted += (sender, e) => Client_DownloadFileCompleted(
sender,
e,
script);
}
public void Client_DownloadFileCompleted(
object sender,
AsyncCompletedEventArgs e,
Script script) {
...
}
谢谢这正是我所需要的,以使这个工作。我们一直在到处寻找答案。