代码之家  ›  专栏  ›  技术社区  ›  dezkev

使用参数调用eventhandler

  •  11
  • dezkev  · 技术社区  · 16 年前

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

    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));
        }
    }
    
    4 回复  |  直到 16 年前
        1
  •  25
  •   Jon Skeet    16 年前

    匿名函数 (匿名方法或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
    }
    
        2
  •  5
  •   Rex M    16 年前

    DownloadDataAsync 具有接受对象的重载:

    DownloadDataAsync(uri, object)
    

    DownloadDataCompleted handler :

    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];
    }
    
        3
  •  2
  •   JDunkerley    16 年前

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

        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
            }
        }
    

        4
  •  1
  •   Alan    13 年前

    Jon Skeet已经回答了这个问题,展示了如何使用lamda表达式,但我仍然不清楚。我还需要更多的例子,最终通过一个按钮找到了这个简单的例子: http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/74d03fe0-0fa5-438d-80e0-cf54fa15af0e

    void A()  
    {  
      Popup parameter = new Popup();  
      buttonClose.Click += (sender, e) => { buttonClose_Click(sender, e, parameter); };  
    }  
    
    static void buttonClose_Click(object sender, EventArgs e, Popup parameter)     
    {     
      MakeSomethingWithPopupParameter(parameter);  
    }
    


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

    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"
    }