现在的位置: 首页 > 综合 > 正文

学习软件设计——C#练习(10)

2018年01月09日 ⁄ 综合 ⁄ 共 4171字 ⁄ 字号 评论关闭

 

C#的事件机制是理解C#的一个难点,尤其对于初学者。我涉世不深,就站在初学者的角度上谈谈我的理解。

先说说,事件机制的情景背景,它就是在一段程序的上下文中。发生了一件事,我通知一个委托人,委托人按照已经定好的办法去处理。翻译成程序语言的表述就是(个人认为这是事件机制的核心).

classA.event+=classB.delegataEventHandler(classC.method)

所以剩下的一切事情都围绕上面三样来做,事件,委托,办法。注意这三样东西可以来自不同的类,下面我以微软的一个例程作为讲解:

using System;

// FireEventArgs: a custom event inherited from EventArgs.

public class FireEventArgs: EventArgs {
    public FireEventArgs(string room,int ferocity) {
        this.room = room;
        this.ferocity = ferocity;
    }

    // The fire event will have two pieces of information--
    // 1) Where the fire is, and 2) how "ferocious" it is. 

    public string room;
    public int ferocity;

}    //end of class FireEventArgs

// Class with a function that creates the eventargs and initiates the event
public class FireAlarm {

    // Events are handled with delegates, so we must establish a FireEventHandler
    // as a delegate:

    public delegate void FireEventHandler(object sender, FireEventArgs fe);
    // Now, create a public event "FireEvent" whose type is our FireEventHandler delegate.

    public event FireEventHandler FireEvent;   

    // This will be the starting point of our event-- it will create FireEventArgs,
    // and then raise the event, passing FireEventArgs.

    public void ActivateFireAlarm(string room,int ferocity) {

        FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

        // Now, raise the event by invoking the delegate. Pass in
        // the object that initated the event (this) as well as FireEventArgs.
        // The call must match the signature of FireEventHandler.

        FireEvent(this, fireArgs);
    }
}    // end of class FireAlarm

// Class which handles the event

class FireHandlerClass {

    // Create a FireAlarm to handle and raise the fire events.

    public FireHandlerClass(FireAlarm fireAlarm)    {

        // Add a delegate containing the ExtinguishFire function to the class'
        // event so that when FireAlarm is raised, it will subsequently execute
        // ExtinguishFire.

        fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);
    }

    // This is the function to be executed when a fire event is raised.

    void ExtinguishFire(object sender, FireEventArgs fe) {

        Console.WriteLine("/nThe ExtinguishFire function was called by {0}.", sender.ToString());

        // Now, act in response to the event.

        if (fe.ferocity < 2)
            Console.WriteLine("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room);
        else if (fe.ferocity < 5)
            Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.",  fe.room);
        else
            Console.WriteLine("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room);
    }
}    //end of class FireHandlerClass

public class FireEventTest {
    public staticvoid Main ()     {   

        // Create an instance of the class that will be firing an event.

        FireAlarm myFireAlarm = new FireAlarm();
       
        // Create an instance of the class that will be handling the event. Note that
        // it receives the class that will fire the event as a parameter.

        FireHandlerClass myFireHandler = new FireHandlerClass(myFireAlarm);
       
        //use our class to raise a few events and watch them get handled
        myFireAlarm.ActivateFireAlarm("Kitchen", 3);
        myFireAlarm.ActivateFireAlarm("Study", 1);
        myFireAlarm.ActivateFireAlarm("Porch", 5);
       
        return;

    }    //end of main

}    // end of FireEventTest

以上这个火灾事件处理程序的关键就是三个地方:

火灾处理委托的定义:

 public delegate void FireEventHandler(object sender, FireEventArgs fe);

火灾这件事发生的定义:

public event FireEventHandler FireEvent;   顺便说一下事件其实就是同签名的委托

在这个函数里面激发事件,即这事发生了

public void ActivateFireAlarm(string room,int ferocity) {

        FireEventArgs fireArgs = new FireEventArgs(room, ferocity);

                FireEvent(this, fireArgs);
    }

火灾发生的处理办法:

 void ExtinguishFire(object sender, FireEventArgs fe) {

        Console.WriteLine("/nThe ExtinguishFire function was called by {0}.", sender.ToString());
        if (fe.ferocity < 2)
            Console.WriteLine("This fire in the {0} is no problem.  I'm going to pour some water on it.", fe.room);
        else if (fe.ferocity < 5)
            Console.WriteLine("I'm using FireExtinguisher to put out the fire in the {0}.",  fe.room);
        else
            Console.WriteLine("The fire in the {0} is out of control.  I'm calling the fire department!", fe.room);
    }
核心语句:把这三样东西关联起来:

 fireAlarm.FireEvent += new FireAlarm.FireEventHandler(ExtinguishFire);

即:事件+=委托(方法);

最后测试:用下面的函数激发事件
myFireAlarm.ActivateFireAlarm("Kitchen", 3);

之后就会自动回调我们实现定义好的方法ExtinguishFire灭火。

所以实现事件机制的关键是,正确定义好三个要素,完成捆绑,并且在使用时正确激发。

抱歉!评论已关闭.