Visual Studio 2008,C# 3.0。
我有一个调用事件处理程序的方法。我想将该方法接收到的两个参数传递给事件处理程序。
我想做这样的事情:
wc.DownloadDataCompleted += wc.DownloadedDataCompleted(strtitle, placeid);
这是否可能,如果是,我将如何去做?
代码片段:
public void downloadphoto(string struri,string strtitle,string placeid)
{
using (WebClient wc = new WebClient())
{
wc.DownloadDataCompleted += wc_DownloadDataCompleted;
wc.DownloadDataAsync(new Uri(struri));
}
}
最简单的方法是使用匿名函数(匿名方法或 lambda 表达式)订阅事件,然后使您的方法仅包含您想要的参数:
public void downloadphoto(string struri, string strtitle, string placeid)
{
using (WebClient wc = new WebClient())
{
wc.DownloadDataCompleted += (sender, args) =>
DownloadDataCompleted(strtitle, placeid, args);
wc.DownloadDataAsync(new Uri(struri));
}
}
// Please rename the method to say what it does rather than where it's used :)
private void DownloadDataCompleted(string title, string id,
DownloadDataCompletedEventArgs args)
{
// Do stuff here
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)