晚上好,
我尝试在DownloadProgressChangedEventHandler的基础上创建自己的EventHandler。
原因是,我想给Callback函数一个额外的参数(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的对话。
这是否有可能像我的想法?

提前致谢。

最佳答案

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

相反,将处理程序包装在另一个处理程序中:

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


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

关于c# - 从DownloadProgressChangedEventHandler创建自己的EventHandler-这可能吗?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/25755830/

10-09 00:31