C#事件-定义事件

来源:互联网 发布:pdf文档解密软件 编辑:程序博客网 时间:2024/05/05 12:15

具体方法:

  1. 定义事件
    • 委托定义:
      public delegate void EventFountion(Param param);
    • 事件定义:
      public event EventFountion eventFountion;
    • 调用事件:
      if (eventFountion!= null) eventFountion(this.param);
  2. 使用事件
    • 事件触发后的方法
      public void a_eventFountion(Param param){
      ...
      }
    • 注册事件
      ClassA a=new ClassA();
      a.eventFountion+=new ClassA.EventFountion(a_eventFountion);

 

实例代码:

  1. 自定义事件
    class ClassA{
       //委托定义:
       public delegate void EventFountion(Param param);
       //事件定义:
       public event EventFountion eventFountion;
      
       //调用事件:
       public void init(){
       if (eventFountion!= null) eventFountion(this.param);
       ...
       }
       ...
    }
  2. 注册事件
    class ClassB{
      
       //调用事件:
       public void fountion(){
       ClassA a=new ClassA();
       a.eventFountion+=new ClassA.EventFountion(a_eventFountion);
       }
       public void a_eventFountion(Param param){
       ....
       }
       ...
    }