晚上好 我尝试在 DownloadProgressChangedEventHandler 的基础上创建自己的 EventHandler。 原因是,我想给回调函数一个额外的参数(文件名)。 这是我的事件处理程序:
public class MyDownloadProgressChangedEventHandler : DownloadProgressChangedEventHandler
{
public object Sender { get; set; }
public DownloadProgressChangedEventArgs E { get; set; }
public string FileName { get; set; }
public MyDownloadProgressChangedEventHandler(object sender, DownloadProgressChangedEventArgs e, string fileName)
{
this.Sender = sender;
this.E = e;
this.FileName = fileName;
}
}
这是我的尝试:
WebClient client = new WebClient();
client.DownloadProgressChanged += new MyDownloadProgressChangedEventhandler(DownloadProgressChanged);
client.DownloadFileAsync(new Uri(String.Format("{0}/key={1}", srv, file)), localName);
Console.WriteLine(String.Format("Download of file {0} started.", localName));
Console.ReadLine();
但是 VS 说从 MyDownloadProgressChangedEventHandler 到 DownloadProgressChangedEventHandler 的对话是不可能的。 有没有可能像我想的那样?
提前致谢。
请您参考如下方法:
WebClient 应该如何知道将什么放入您定义的变量中? (不能)
相反,将您获得的处理程序包装在另一个处理程序中:
string fileName = new Uri(String.Format("{0}/key={1}", srv, file));
client.DownloadProgressChanged +=
(sender, e) =>
DownloadProgressChanged(sender, e, fileName);
client.DownloadFileAsync(fileName);
在这些情况下使用 Lambda 表达式总是很有趣!
