如何通过一个事件的方法? [英] How to pass an event to a method?

查看:117
本文介绍了如何通过一个事件的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个需要一个事件作为一个参数,并增加了事件处理程序,以它来正确地处理它的方法。像这样的:

我有两个事件:

 公共事件的EventHandler点击;
公共事件的EventHandler CLICK2;

现在我想一个特定的事件传递给我的方法是这样的(伪code):

 公共AttachToHandleEvent(事件处理MyEvent)
{
    MyEvent + = ITEM_CLICK;
}私人无效ITEM_CLICK(对象发件人,EventArgs的发送)
{
    的MessageBox.show(LALALA);
}ToolStripMenuItem工具=新ToolStripMenuItem();
AttachToHandleEvent(tool.Click);

这可能吗?

我注意到,这code正常工作,并返回到我的项目,并注意到,当我通过我的类中声明的事件,它的工作原理,但是当我通过从其他类的事件仍然无法正常工作

我得到的是这个错误:


  

事件
  System.Windows.Forms.ToolStripItem.Click
  只能出现在左手侧
  的+ =或 - =



解决方案

我原来的答复是合适的,从定义事件类中,但你既然更新了问题,以反映你希望从外面做到这一点定义类,所以我认为欲绝

仅定义一个事件可以指该事件使用隐式代表变量的类。从该类之外,你只能访问到添加删除方法,通过 + = - = 。这意味着,你不能做你问什么,直接。你可以,但是,使用功能的方法。

  A级{
    公共事件的EventHandler事件1;    公共无效TriggerEvent1(){
        如果(事件1!= NULL)
            事件1(这一点,EventArgs.Empty);
    }
}B类{
    静态无效的handleEvent(对象o,EventArgs的发送){
        Console.WriteLine(呜 - 呼!);
    }    静态无效AttachToEvent(动作<&事件处理GT;附加){
        附加(为handleEvent);
    }    静态无效的主要(){
        A中新= A();
        AttachToEvent(处理器= GT; a.Event1 + =处理程序);
        a.TriggerEvent1();
    }
}

I would like to create a method that takes an event as an argument and adds eventHandler to it to handle it properly. Like this:

I have two events:

public event EventHandler Click;
public event EventHandler Click2;

Now I would like to pass a particular event to my method like this (pseudocode):

public AttachToHandleEvent(EventHandler MyEvent)
{
    MyEvent += Item_Click;
}

private void Item_Click(object sender, EventArgs e)
{
    MessageBox.Show("lalala");
}

ToolStripMenuItem tool = new ToolStripMenuItem();
AttachToHandleEvent(tool.Click);

Is it possible?

I've noticed that this code worked fine, and returned to my project and noticed that when I pass an event declared in my class, it works, but when I pass event from other class it still does not work.

What I get is this error:

The event 'System.Windows.Forms.ToolStripItem.Click' can only appear on the left hand side of += or -=

解决方案

My original answer was suitable from within the class that defined the event, but you've since updated your question to reflect that you wish to accomplish this from outside the defining class, so I've stricken that.

Only the class that defines an event can refer to the implicit delegate variable that the event uses. From outside that class, you only have access to the add and remove methods, via += and -=. This means that you can't do what you're asking, directly. You can, however, use a functional approach.

class A{
    public event EventHandler Event1;

    public void TriggerEvent1(){
        if(Event1 != null)
            Event1(this, EventArgs.Empty);
    }
}

class B{
    static void HandleEvent(object o, EventArgs e){
        Console.WriteLine("Woo-hoo!");
    }

    static void AttachToEvent(Action<EventHandler> attach){
        attach(HandleEvent);
    }

    static void Main(){
        A a = new A();
        AttachToEvent(handler=>a.Event1 += handler);
        a.TriggerEvent1();
    }
}

这篇关于如何通过一个事件的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆