zoukankan      html  css  js  c++  java
  • C# 事件机制归纳(转载)

    1.委派的实现过程。

    首先来看一下委派,委派其实就是方法的传递,并不定义方法的实现。事件其实就是标准化了的委派,为了事件处理过程特制的、稍微专业化一点的组播委派(多点委派)。下面举一个例子,我觉得把委派的例子和事件的例子比较,会比较容易理解。

    using System;

    class Class1

    {

    delegate int MathOp(int i1,int i2);

    static void Main(string[] args)

    {

    MathOp op1=new MathOp(Add);

    MathOp op2=new MathOp(Multiply);

    Console.WriteLine(op1(100,200));

    Console.WriteLine(op2(100,200));

    Console.ReadLine();

    }

    public static int Add(int i1,int i2)

    {

    return i1+i2;

    }

    public static int Multiply(int i1,int i2)

    {

    return i1*i2;

    }

    }

    首先代码定义了一个委托MathOp,其签名匹配与两个函数Add()和Multiply()的签名(也就是其带的参数类型数量相同):


    delegate int MathOp(int i1,int i2);

    Main()中代码首先使用新的委托类型声明一个变量,并且初始化委托变量.注意,声明时的参数只要使用委托传递的函数的函数名,而不加括号:

    MathOp op1=new MathOp(Add);

    (或为MathOp op1=new MathOp(Multiply);)

    委托传递的函数的函数体:

    public static int Add(int i1,int i2)

    {

    return i1+i2;

    }

    public static int Multiply(int i1,int i2)

    {

    return i1*i2;

    }

    然后把委托变量看作是一个函数名,将参数传递给函数。 Console.WriteLine(op1(100,200));


    Console.WriteLine(op2(100,200));

    2.事件的实现过程

    using System;

    class Class1

    {
    static void Main(string[] args)

    {

    Student s1=new Student();

    Student s2=new Student();

    s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

    s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

    s1.Register();

    s2.Register();

    Console.ReadLine();

    }

    static void Student_RegisterOK()

    {

    Console.WriteLine("Hello");

    }

    }

    class Student

    {

    public delegate void DelegateRegisterOkEvent();

    public event DelegateRegisterOkEvent RegisterOK;
    public string Name;

    public void Register()

    {

    Console.WriteLine("Register Method");

    RegisterOK();

    }

    }

    在Student类中,先声明了委托DelegateRegisterOkEvent(),然后使用event和要使用的委托类型(前面定义的DelegateRegisterOkEvent委托类型)声明事件RegisterOK(可以看作是委托的一个实例。):

    public delegate void DelegateRegisterOkEvent();

    public event DelegateRegisterOkEvent RegisterOK;

    然后在Main()函数中,实例化Student类,然后s1.RegisterOK事件委托给了Student_RegisterOK 方法。通过“+=”(加等于)操作符非常容易地为.Net对象中的一个事件添加一个甚至多个响应方法;还可以通过非常简单的“-=”(减等于)操作符取消这些响应方法。

    然后,当调用s1.Register()时,事件s1.RegisterOK发生。

    3.C#中预定义事件处理方式

    学习事件,我觉得最不好理解的就是C#中预定义了事件,使我才开始学习事件时一头雾水。在查了些资料后,终于弄明白了一些,如下:

    EventArgs是包含事件数据的类的基类,用于传递事件的细节。

    EventHandler是一个委托声明如下(其在.Net类库中如下声明的)

    public delegate void EventHandler( object sender , EventArgs e )

    所以,所有形如:

    void 函娄名(object 参数名,EventArgs 参数名);的函数都可以作为Control类的Click事件响应方法了。如下面所定义的一个事件响应方法:

    private void button1_Click(object sender, System.EventArgs e)

    参数object sender表示引发事件的对象,(其实这里传递的是对象的引用,如果是button1的click事件则sender就是button1)System.EventArgs e 代表事件的相应信息,如鼠标的x,y值等。

    下面我们研究一下Button类看看其中的事件声明,以Click事件为例。

    public event EventHandler Click;

    这里定义了一个EventHandler类型的事件Click

    private void button1_Click(object sender, System.EventArgs e)
    {
    ...
    }

    这是我们和button1_click事件所对应的方法。注意方法的参数符合委托中的签名(既参数列表)。那我们怎么把这个方法和事件联系起来呢,请看下面的代码。
    this.button1.Click += new System.EventHandler(this.button1_Click); (其实button1.Click 为System.EventHandler委派的实例事件。与委派中委派实例委托给某一方法非常相似)
    把this.button1_Click方法绑定到this.button1.Click事件。

    4.事件的参数的使用。

    using System;

    class Class1

    {
    static void Main()
    {
    Student s1=new Student();

    s1.Name ="Student1";

    Student s2=new Student();

    s2.Name ="Student2";

    s1.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

    s2.RegisterOK +=new Student.DelegateRegisterOkEvent(Student_RegisterOK);

    //当Register方法一执行,触发RegisterOK事件

    //RegisterOK事件一触发,然后执行Student_RegisterOK方法

    s1.Register();

    s2.Register();

    Console.ReadLine();
    }
    static void Student_RegisterOK(RegisterOkArgs e)
    {
    Console.WriteLine(e.EventInfo);
    }
    }
    class Student

    {

    public delegate void DelegateRegisterOkEvent(RegisterOkArgs e);

    public event DelegateRegisterOkEvent RegisterOK;

    public string Name;

    public void Register()

    {
    Console.WriteLine("Register Method");
    RegisterOK(new RegisterOkArgs("Student Name: "+Name));
    }
    }

    class RegisterOkArgs:EventArgs

    {
    public string EventInfo;

    public RegisterOkArgs(string eventInfo):base()
    {
    this.EventInfo =eventInfo;
    }
    }


    转载: http://www.builder.com.cn/2008/0516/865779.shtml

  • 相关阅读:
    yum install nginx
    逻辑分区增加空间 vm中
    pbspro build rpm and installation
    centos 6 and 7 to modify hostname
    activeMQ
    cgo在mac上编译
    redis学习
    Spring Boot 2+gRPC 学习系列1:搭建Spring Boot 2+gRPC本地项目
    KumuluzEE
    前端实现“查看更多”效果
  • 原文地址:https://www.cnblogs.com/JoshuaDreaming/p/2167807.html
Copyright © 2011-2022 走看看