zoukankan      html  css  js  c++  java
  • C#事件DEMO

    举个简单的流程
    1.先定义一个委托,不过很多时候不需要,可以直接使用系统的。
    public delegate void SampleEventHandler(object sender, SampleEventArgs e);
    2.在类中定义事件。
    public event SampleEventHandler SampleEvent;
    要了解深刻一点,你可以查看一下多播委托(Multicast Delegate)
    3.执行事件:
    private OnSampleEventHandler(object sender, SampleEventArgs e) {
        if (SampleEvent != null) {
            SampleEvent(sender, e);
        }
    }
    外部:
    4.加载事件:
    xxx.SampleEvent += this.xxx_OnSampleEventHandler;
    5.定义事件发生时执行的方法
    protected void xxx_OnSampleEventHandler(object sender, SampleEventArgs e) { ... }
    View Code
            private void Form1_Load(object sender, EventArgs e)
            {
                Publisher pub = new Publisher();
                Receiver rec = new Receiver();
                pub.delegtevent += new DelegteEvent(rec.ReceiverHandler);  //注册
                pub.FireEvent();//触发事件
                Console.Read();
            }
        }
        //声明代理
        public delegate void DelegteEvent(object sender,MyEventArgs e); //声明一个代理类
       
        
        
        
        public class Publisher
        {
            //定义事件
            public event DelegteEvent delegtevent;
            //事件触发的方法
            public virtual void FireEvent(MyEventArgs e)
            {
                if (delegtevent != null)
                    //调用委托触发事件
                    delegtevent(this,e);    
            }
        }
    
    
       
    
        public class Receiver
        {
            //事件处理函数
            public void ReceiverHandler(object sender,MyEventArgs e)
            {
                Console.WriteLine("ReceiverHandler is {0}",e._salary);
            }
        }
    
        public class MyEventArgs : EventArgs         //定义事件参数类
        {
            public readonly double _salary;
            public MyEventArgs(double salary)
            {
                this._salary = salary;
            }
        }

    案例

    View Code
       using System; 
    
    using System.Collections.Generic; 
    
    using System.Text;
    
    namespace Delegate {
    
            // 热水器
    
            public class Heater {
    
                   private int temperature;
    
                   public string type = "RealFire 001";
    
                  // 添加型号作为演示
    
                   public string area = "China Xian";
    
                         // 添加产地作为演示
    
                   //声明委托
    
                   public delegate void BoiledEventHandler(Object sender, BoliedEventArgs e);
    
                   public event BoiledEventHandler Boiled; 
    
          //声明事件
    
                  // 定义BoliedEventArgs类,传递给Observer所感兴趣的信息
    
                   public class BoliedEventArgs : EventArgs 
                   {
    
                          public readonly int temperature;
    
                          public BoliedEventArgs(int temperature)
                          {
    
                                 this.temperature = temperature;
    
                          }
    
                   }
    
                  // 可以供继承自 Heater 的类重写,以便继承类拒绝其他对象对它的监视
    
                   protected virtual void OnBolied(BoliedEventArgs e)
                   {
    
                          if (Boiled != null) 
                          {
    
           // 如果有对象注册
    
                                 Boiled(this, e);
    
           // 调用所有注册对象的方法
    
                          }
    
                   }
    
                                 // 烧水。
    
                   public void BoilWater() 
                   {
    
                          for (int i = 0; i <= 100; i++) 
    
                                 temperature = i;
    
                                 if (temperature > 95)
                                 { 
    
                                       //建立BoliedEventArgs 对象。
    
                                        BoliedEventArgs e = new BoliedEventArgs(temperature);
    
                                        OnBolied(e);       // 调用 OnBolied方法
    
                                 }
    
                          }
    
                   }
    
            }
    
           // 警报器
    
            public class Alarm
            {
    
                   public void MakeAlert(Object sender, Heater.BoliedEventArgs e) 
                   
                   {
    
                          Heater heater = (Heater)sender; 
    
                 //这里是不是很熟悉呢?
    
                          //访问 sender 中的公共字段
    
                          Console.WriteLine("Alarm:{0} - {1}: ", heater.area, heater.type);
    
                          Console.WriteLine("Alarm: 嘀嘀嘀,水已经 {0} 度了:", e.temperature);
    
                          Console.WriteLine(); 
    
                  }
    
           }
    
           // 显示器
    
            public class Display 
            {
    
                   public static void ShowMsg(Object sender, Heater.BoliedEventArgs e)
                   {
    
                     //静态方法
    
                          Heater heater = (Heater)sender; 
    
                         Console.WriteLine("Display:{0} - {1}: ", heater.area, heater.type);
    
                          Console.WriteLine("Display:水快烧开了,当前温度:{0}度。", e.temperature);
    
                          Console.WriteLine(); 
    
                  }
    
            }
    
           class Program {
    
                   static void Main() {
    
                          Heater heater = new Heater(); 
    
                         Alarm alarm = new Alarm();
    
                         heater.Boiled += alarm.MakeAlert;
    
           //注册方法
    
                          heater.Boiled += (new Alarm()).MakeAlert;
    
                  //给匿名对象注册方法
    
                          heater.Boiled += new Heater.BoiledEventHandler(alarm.MakeAlert);
    
           //也可以这么注册
    
                          heater.Boiled += Display.ShowMsg; 
    
                 //注册静态方法
    
                         heater.BoilWater();
    
           //烧水,会自动调用注册过对象的方法
    
                   } 
    
           }
    
    }

     总结

    View Code
    1//声明委托
     public delegate void MyEventHandler(Object sender, MyEventArgs e);
    2//声明事件
    public event MyEventHandler MyEvent;
    3// 定义MyEventArgs类,传递给Observer所感兴趣的信息
    public class MyEventArgs : EventArgs 
                   {
                          public readonly int interestFiles;
                          public BoliedEventArgs(int interestFiles)
                          {
                                 this.interestFiles = interestFiles;
                          }
                   }
    4// 可以供继承类重写,以便继承类拒绝其他对象对它的监视
                   protected virtual void MyEvent (MyEventArgs e)
                   {
                          if (MyEvent!= null) 
                          {
                             // 如果有对象注册
                                 MyEvent (this, e);        
                            // 调用所有注册对象的方法
                          }
                   }
    
    5// 声明触发事件的方法。
                   public void tiggerMethod() 
                   {
                          for (int i = 0; i <= 100; i++) 
                                 interestFiles = i;
                                 if (interestFiles > 95)
     { 
                                   //建立BoliedEventArgs 对象。
                                   MyEventArgs e = new MyEventArgs(interestFiles);
    // 调用 MyEvent方法                                
     MyEvent(e);      
                                 }
                          }
                   }
  • 相关阅读:
    tomcat快速部署脚本
    Centos7下搭建单节点Zookeeper
    request的基本使用用法
    Centos7 下配置jdk
    oracle备份脚本(以日期命名文件夹)
    Centos7 关闭防火墙
    Centos7 配置静态ip地址
    杀死占用8080端口的进程
    git clone的时候遇到“Please make sure you have the correct access rights and the repository exists”
    【笔记】关于多分类问题中的混淆矩阵,精准率
  • 原文地址:https://www.cnblogs.com/anbylau2130/p/2746006.html
Copyright © 2011-2022 走看看