C#(067):事件event(C # (067): Event)

一、概述

委托是类型,事件是成员,事件其实是类中一个private的委托变量和add,remove两个方法。子类无法调用父类的事件。

二、使用

  • 事件数据类
  • 定义委托
  • 声明委托变量
  • 对象类
  • 调用
    delegate void CharEventHandler(object sender, CharEventArgs e); //定义委托
    
    public class CharEventArgs : EventArgs //定义一个事件数据类
    {
        public char CurrChar;
        public CharEventArgs(char CurrChar)
        {
            this.CurrChar = CurrChar;
        }
    }
    
    class CharChecker
    {
        char curr_char;
        public event CharEventHandler CharTest;//公布事件(声明委托变量)
        public char Curr_Char
        {
            get { return curr_char; }
            set
            {
                if (CharTest != null)
                {
                    CharEventArgs args = new CharEventArgs(value);
                    CharTest(this, args);// 触发事件(执行委托方法),传递实际参数值到事件处理程序。
                    curr_char = args.CurrChar;
                }
            }
        }
    }
    static void Main()
    {
        CharChecker chartester = new CharChecker();
        chartester.CharTest += (Source, e) =>//将事件与事件处理函数绑定(实例化委托)
        {
            {
                if (e.CurrChar == 'x' || e.CurrChar == 'X')
                {
                    Console.Write("触发的字符是x,");
                    Console.Write("把x替为:");
                    e.CurrChar = '?';
                }
                else
                    Console.Write("触发的字符不是x,");
            }
        };
        chartester.Curr_Char = 'a'; //设置属性值时,触发事件
        Console.WriteLine("事件处理结果:{0}", chartester.Curr_Char);
        
        chartester.Curr_Char = 'b';
        Console.WriteLine("事件处理结果:{0}", chartester.Curr_Char);
        
        chartester.Curr_Char = 'x';
        Console.WriteLine("{0}", chartester.Curr_Char);
        Console.WriteLine();
    }

三、使用泛型委托 EventHandler

在.Net2.0以上,可用泛型,而不需要定义charEventHandler委托。

    public event EventHandler<CharEventArgs> CharTest;
————————

1、 Overview

A delegate is a type and an event is a member. An event is actually a private delegate variable and two methods of add and remove in the class. A subclass cannot call an event of a parent class.

2、 Use

  • Event data class
  • Define delegation
  • Declare delegate variables
  • Object class
  • call
    delegate void CharEventHandler(object sender, CharEventArgs e); //定义委托
    
    public class CharEventArgs : EventArgs //定义一个事件数据类
    {
        public char CurrChar;
        public CharEventArgs(char CurrChar)
        {
            this.CurrChar = CurrChar;
        }
    }
    
    class CharChecker
    {
        char curr_char;
        public event CharEventHandler CharTest;//公布事件(声明委托变量)
        public char Curr_Char
        {
            get { return curr_char; }
            set
            {
                if (CharTest != null)
                {
                    CharEventArgs args = new CharEventArgs(value);
                    CharTest(this, args);// 触发事件(执行委托方法),传递实际参数值到事件处理程序。
                    curr_char = args.CurrChar;
                }
            }
        }
    }
    static void Main()
    {
        CharChecker chartester = new CharChecker();
        chartester.CharTest += (Source, e) =>//将事件与事件处理函数绑定(实例化委托)
        {
            {
                if (e.CurrChar == 'x' || e.CurrChar == 'X')
                {
                    Console.Write("触发的字符是x,");
                    Console.Write("把x替为:");
                    e.CurrChar = '?';
                }
                else
                    Console.Write("触发的字符不是x,");
            }
        };
        chartester.Curr_Char = 'a'; //设置属性值时,触发事件
        Console.WriteLine("事件处理结果:{0}", chartester.Curr_Char);
        
        chartester.Curr_Char = 'b';
        Console.WriteLine("事件处理结果:{0}", chartester.Curr_Char);
        
        chartester.Curr_Char = 'x';
        Console.WriteLine("{0}", chartester.Curr_Char);
        Console.WriteLine();
    }

三、使用泛型委托 EventHandler

Yes Net2. Above 0, generics can be used instead of defining chareventhandler delegate.

    public event EventHandler<CharEventArgs> CharTest;