C# 使用参数调用eventhandler

C# 使用参数调用eventhandler,c#,events,handlers,C#,Events,Handlers,Visual Studio 2008,C#3.0 我在下面有一个调用事件处理程序的方法。我想将该方法接收到的两个参数传递给事件处理程序 我想这样做: wc.DownloadDataCompleted += wc.DownloadedDataCompleted(strtitle, placeid); private void TreeViewCreateContextMenu(TreeNode node) { ContextMenuStrip contextMenu = new Cont

Visual Studio 2008,C#3.0

我在下面有一个调用事件处理程序的方法。我想将该方法接收到的两个参数传递给事件处理程序

我想这样做:

wc.DownloadDataCompleted += wc.DownloadedDataCompleted(strtitle, placeid);
private void TreeViewCreateContextMenu(TreeNode node)
{
    ContextMenuStrip contextMenu = new ContextMenuStrip();

    // create the menu items
    ToolStripMenuItem newMenuItem = new ToolStripMenuItem();
    newMenuItem.Text = "New...";

    // add the menu items to the menu
    contextMenu.Items.AddRange(new ToolStripMenuItem[] { newMenuItem });

    // add its event handler using a lambda expression, passing
    //  the additional parameter "myData"
    string myData = "This is the extra parameter.";
    newMenuItem.Click += (sender, e) => { newMenuItem_Click(sender, e, myData); };

    // finally, set the node's context menu
    node.ContextMenuStrip = contextMenu;
}

// the custom event handler, with "extraData":
private void newMenuItem_Click(object sender, EventArgs e, string extraData)
{
    // do something with "extraData"
}
这可能吗?如果可能的话,我该怎么做

代码段:

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
}

DownloadDataAsync
有一个重载,它接受一个对象:

DownloadDataAsync(uri, object)
该对象可以是传递到
DownloadDataCompleted处理程序中的任意对象:

public void downloadphoto(string struri,string strtitle,string placeid)
{
    using (WebClient wc = new WebClient())
    {
        string[] data = new string[2] { strtitle, placeid };
        wc.DownloadDataCompleted += wc_DownloadDataCompleted;
        wc.DownloadDataAsync(new Uri(struri), data);
    }
}


void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
    string[] data = (string[])e.UserToken;
    string strtitle = data[0];
    string placeid = data[1];
}

您可以创建一个私有类并将处理程序放在其中。例如

    public void downloadphoto(string struri, string strtitle, string placeid)
    {
        using (WebClient wc = new WebClient())
        {
            wcHandler handler = new wcHandler() { Strtitle = strtitle, Placeid = placeid };
            wc.DownloadDataCompleted += handler.wc_DownloadDataCompleted;
            wc.DownloadDataAsync(new Uri(struri));
        }

    }

    private class wcHandler
    {
        public string Strtitle { get; set; }
        public string Placeid { get; set; }

        public void wc_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
        {
            // Do Stuff
        }
    }

不过,考虑到乔恩回答的优雅,他可能会用这个

Jon Skeet已经回答了这个问题,展示了如何使用lamda表达式,但我仍然不清楚。我还需要更多的例子,最终通过一个按钮找到了这个简单的例子:


在我的例子中,我使用了TreeView控件的上下文菜单,结果如下所示:

wc.DownloadDataCompleted += wc.DownloadedDataCompleted(strtitle, placeid);
private void TreeViewCreateContextMenu(TreeNode node)
{
    ContextMenuStrip contextMenu = new ContextMenuStrip();

    // create the menu items
    ToolStripMenuItem newMenuItem = new ToolStripMenuItem();
    newMenuItem.Text = "New...";

    // add the menu items to the menu
    contextMenu.Items.AddRange(new ToolStripMenuItem[] { newMenuItem });

    // add its event handler using a lambda expression, passing
    //  the additional parameter "myData"
    string myData = "This is the extra parameter.";
    newMenuItem.Click += (sender, e) => { newMenuItem_Click(sender, e, myData); };

    // finally, set the node's context menu
    node.ContextMenuStrip = contextMenu;
}

// the custom event handler, with "extraData":
private void newMenuItem_Click(object sender, EventArgs e, string extraData)
{
    // do something with "extraData"
}

Jon的版本很时髦,但是它可以工作并且兼容非3.5版本。@womp Jon的版本很时髦,但是它可以工作并且兼容非3.5版本:),所以我们同意。这真是太棒了。我只想澄清一下:我的版本需要C#3,而不是.NET3.5。要使用C#2,只需使用匿名方法而不是lambda表达式。我需要使用e.UserState,因为DownloadDataCompletedEventArgsAwesome中没有UserToken!!现在谁会想到这个!!如何取消订阅该事件?@jdelator:您需要将委托存储在一个变量中,然后使用该变量取消订阅。