1

我有四个事件:

View.AdditionPerformed += new EventHandler<EventArgs>(OnOperationPerformed);
View.SubtractionPerformed+=new EventHandler<EventArgs>(OnOperationPerformed);
View.DivisionPerformed+=new EventHandler<EventArgs>(OnOperationPerformed);
View.MultiplyPerformed+=new EventHandler<EventArgs>(OnOperationPerformed);

和一种方法:

private void OnOperationPerformed(object sender, EventArgs e)
 {

 } 

如何定义哪个事件引发了我的方法?像这样的东西:

private void OnOperationPerformed(object sender, EventArgs e)
 {
    switch(event)
     {
      case MultiplyPerformed:{}
      case DivisionPerformed:{}
      ...
     }  
 } 
4

2 回答 2

4

写你自己的EventArgs,里面有一个enum,告诉你引发的事件。

enum MyEventEnum
{
    AdditionPerformed,
    SubtractionPerformed,
    DivisionPerformed,
    MultiplayPerformed
}

EventArgs

class MyEventArgs : EventArgs
{
    public MyEventEnum EventRaised { get; set; }
}

定义处理程序

View.AdditionPerformed += new EventHandler<MyEventArgs>(OnOperationPerformed);
View.SubtractionPerformed+=new EventHandler<MyEventArgs>(OnOperationPerformed);
View.DivisionPerformed+=new EventHandler<MyEventArgs>(OnOperationPerformed);
View.MultiplyPerformed+=new EventHandler<MyEventArgs>(OnOperationPerformed);

当你打电话给他们时:

this.AdditionPerformed(this, new MyEventArgs 
    { EventRaised = MyEventEnum.AdditionPerformed };

我知道它是硬编码的,但没有其他方法。

于 2012-10-23T09:00:08.060 回答
0

除了使用 EventArgs,您可以使用自己的事件参数类来传递必要的数据,以便在处理程序中进行选择。

然后它将在处理程序内的 e 变量上可用。

干杯

于 2012-10-23T08:57:36.997 回答