C# event with custom arguments

前端 未结 7 1492
庸人自扰
庸人自扰 2020-12-05 09:29

I want to have an event that takes an enum only as the argument. For example

public enum MyEvents{
   Event1
}

// how do I declare this to take enum MyEvent         


        
相关标签:
7条回答
  • 2020-12-05 10:17

    EventHandler receives EventArgs as a parameter. To resolve your problem, you can build your own 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 });
         } 
     } 
    
    0 讨论(0)
提交回复
热议问题