1

こんばんは、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 への会話は不可能であると言います。これは私が思うように可能ですか?

前もって感謝します。

4

1 に答える 1

4

WebClient は、定義された変数の中に何を入れるかをどのように知る必要がありますか? (できません)

代わりに、取得したハンドラーを別のハンドラーにラップします。

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

ラムダ式は、これらの状況で使用するのが常に楽しいです!

于 2014-09-10T01:12:46.477 に答える