从DownloadProgressChangedEventHandler创建自己的EventHandler-这可能吗

本文关键字:EventHandler- DownloadProgressChangedEventHandler 创建 自己的 | 更新日期: 2023-09-27 18:21:13

晚上好,我尝试在DownloadProgressChangedEventHandler的基础上创建一个自己的EventHandler。原因是,我想给回调函数一个额外的参数(fileName)。这是我的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的对话是不可能的。这可能像我想的那样吗?

提前谢谢。

从DownloadProgressChangedEventHandler创建自己的EventHandler-这可能吗

WebClient应该如何知道在定义的变量中放入什么?(不能)

相反,把你得到的处理程序包在另一个里面:

string fileName = new Uri(String.Format("{0}/key={1}", srv, file));
client.DownloadProgressChanged += 
    (sender, e) =>
        DownloadProgressChanged(sender, e, fileName);
client.DownloadFileAsync(fileName);

Lambda表达式在这种情况下使用总是很有趣的!