C# 从DownloadProgressChangedEventHandler创建自己的EventHandler-这可能吗?
晚上好,, 我尝试在下载的ProgressChangedEventHandler的基础上创建自己的EventHandler。 原因是,我想给回调函数一个额外的参数(文件名)。 这是我的EventHandler:C# 从DownloadProgressChangedEventHandler创建自己的EventHandler-这可能吗?,c#,delegates,eventhandler,C#,Delegates,Eventhandler,晚上好,, 我尝试在下载的ProgressChangedEventHandler的基础上创建自己的EventHandler。 原因是,我想给回调函数一个额外的参数(文件名)。 这是我的EventHandler: public class MyDownloadProgressChangedEventHandler : DownloadProgressChangedEventHandler { public object Sender { get; set; }
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表达式在这些情况下使用总是很有趣的 WebClient如何知道在定义的变量中放入什么?(它不能) 相反,将得到的处理程序包装到另一个处理程序中:
string fileName = new Uri(String.Format("{0}/key={1}", srv, file));
client.DownloadProgressChanged +=
(sender, e) =>
DownloadProgressChanged(sender, e, fileName);
client.DownloadFileAsync(fileName);
Lambda表达式在这些情况下使用总是很有趣的 是的,当涉及到继承时,委托与普通类相比表现得很奇怪:)(它是反向的)而且,通常更容易创建一个完整的新委托。是的,当涉及到继承时,委托与普通类相比表现得很奇怪:)(它是反向的)同样,通常更容易创建一个完整的新委托。