带有自定义参数的C#事件

时间:2010-09-18 14:20:29

标签: c# events

我想要一个仅将enum作为参数的事件。例如

public enum MyEvents{
   Event1
}

// how do I declare this to take enum MyEvents as argument
public static event EventHandler EventTriggered;

public static void Trigger(MyEvent event )
{
    if (EventTriggered != null)
    {
        EventTriggered(event);
    }
}

7 个答案:

答案 0 :(得分:61)

EventHandler收到EventArgs作为参数。要解决您的问题,您可以构建自己的MyEventArgs

 public enum MyEvents
 { 
    Event1 
 }

 public class MyEventArgs : EventArgs
 {
    public MyEvents MyEvent { get; set; }
 }

 public static event EventHandler<MyEventArgs> EventTriggered; 

 public static void Trigger(MyEvents ev) 
 { 
     if (EventTriggered != null) 
     {
         EventTriggered(null, new MyEventArgs { MyEvent = ev });
     } 
 } 

答案 1 :(得分:21)

您为参数声明了委托:

public enum MyEvents { Event1 }

public delegate void MyEventHandler(MyEvents e);

public static event MyEventHandler EventTriggered;

虽然框架中的所有事件都采用了EventArgs的参数,但您可以使用您喜欢的任何参数。但是,人们可能会期望框架中使用的模式,这可能会使您的代码更难以遵循。

答案 2 :(得分:12)

我可能会在游戏中迟到,但是如何:

public event Action<MyEvent> EventTriggered = delegate { }; 

private void Trigger(MyEvent e) 
{ 
     EventTriggered(e);
} 

将事件设置为匿名委托可以避免我检查事件是否为空。

我发现这在使用MVVM时很方便,就像使用ICommand.CanExecute方法一样。

答案 3 :(得分:5)

您需要声明自定义事件处理程序。

public class MyEventArgs: EventArgs
{
  ...
}

public delegate void MyEventHandler(object sender, MyEventArgs e);

public class MyControl: UserControl
{
  public event MyEventHandler MyEvent;
   ...
}

答案 4 :(得分:3)

这是您的样本的改造,以帮助您入门。

  • 你的样本有一个静态事件 - 事件来自一个类实例更常见,但我在下面保持静态。

  • 下面的示例还使用更标准的命名OnXxx来提升事件。

  • 下面的示例不考虑线程安全性,如果您坚持认为事件是静态的,这可能会成为一个问题。

public enum MyEvents{ 
     Event1 
} 

public class MyEventArgs : EventArgs
{
    public MyEventArgs(MyEvents myEvents)
    {
        MyEvents = myEvents;
    }

    public MyEvents MyEvents { get; private set; }
}

public static class MyClass
{
     public static event EventHandler<MyEventArgs> EventTriggered; 

     public static void Trigger(MyEvents myEvents) 
     {
         OnMyEvent(new MyEventArgs(myEvents));
     }

     protected static void OnMyEvent(MyEventArgs e)
     {
         if (EventTriggered != null)
         {
             // Normally the first argument (sender) is "this" - but your example
             // uses a static event, so I'm passing null instead.
             // EventTriggered(this, e);
             EventTriggered(null, e);
         } 
     }
}

答案 5 :(得分:1)

public enum MyEvents
{
    Event1
}

public class CustomEventArgs : EventArgs
{
    public MyEvents MyEvents { get; set; }
}


private EventHandler<CustomEventArgs> onTrigger;

public event EventHandler<CustomEventArgs> Trigger
{
    add
    {
        onTrigger += value;
    }
    remove
    {
        onTrigger -= value;
    }
}

protected void OnTrigger(CustomEventArgs e)
{
    if (onTrigger != null)
    {
        onTrigger(this, e);
    }
}

答案 6 :(得分:0)

没有参数的示例:

delegate void NewEventHandler();
public event NewEventHandler OnEventHappens;

从另一个班级开始,您可以订阅

otherClass.OnEventHappens += ExecuteThisFunctionWhenEventHappens;

并声明不带参数的函数。