委托与事件

c#设计

********************************     委托       ***********************************


(1)委托:委托是一种引用方法的类型。一旦为委托分配了方法,委托将与该方法具有完全相同的行为。委托方法的使用可以像其他任何方法一样,具有参数和返回值
与委托的签名(由返回类型和参数组成)匹配的任何方法都可以分配给该委托。这样就可以通过编程方式来更改方法调用,还可以向现有类中插入新代码。只要知道委托的签名,便可以分配自己的委托方法。

将方法作为参数进行引用的能力使委托成为定义回调方法的理想选择。例如,可以向排序算法传递对比较两个对象的方法的引用。分离比较代码使得可以采用更通用的方式编写算法。

 

示例 1
以下是声明及使用委托的一个简单示例。注意,委托 Del 和关联的方法 MultiplyNumbers 具有相同的签名

// Declare a delegate
delegate void Del(int i, double j);

class MathClass
{
    static void Main()
    {
        MathClass m = new MathClass();

        // Delegate instantiation using "MultiplyNumbers"
        Del d = m.MultiplyNumbers;

        // Invoke the delegate object.
        System.Console.WriteLine("Invoking the delegate using 'MultiplyNumbers':");
        for (int i = 1; i <= 5; i++)
        {
            d(i, 2);
        }
    }

    // Declare the associated method.
    void MultiplyNumbers(int m, double n)
    {
        System.Console.Write(m * n + " ");
    }
}


示例 2
在下面的示例中,一个委托被同时映射到静态方法和实例方法,并分别返回特定的信息。

// Declare a delegate
delegate void Del();

class SampleClass
{
    public void InstanceMethod()
    {
        System.Console.WriteLine("A message from the instance method.");
    }

    static public void StaticMethod()
    {
        System.Console.WriteLine("A message from the static method.");
    }
}

class TestSampleClass
{
    static void Main()
    {
        SampleClass sc = new SampleClass();

        // Map the delegate to the instance method:
        Del d = sc.InstanceMethod;
        d();

        // Map to the static method:
        d = SampleClass.StaticMethod;
        d();
    }
}

输出
A message from the instance method.

A message from the static method.


示例 3
委托链的应用


delegate void EatDelegate(string food);

class DelegateMath
{
  static void zsEat(string food)
  {
    Console.WriteLine("张三"+food);
  }
 
  static void lsEat(string food)
  {
    Console.WriteLine("李四"+food);
  }
 
  static void wwEat(string food)
  {
    Console.WriteLine("王五"+food);
  }
 
  static void Main()
  {
    //EatDelegate d = DelegateMath.zsEat;d("西瓜");
   
    EatDelegate zs = new EatDelegate(zsEat);
    EatDelegate ls = new EatDelegate(lsEat);
    EatDelegate ww = new EatDelegate(wwEat);
    EatDelegate delegateChain;
    delegateChain = zs+ls+ww;
    delegateChain("西瓜");
    delegateChain -= ww;
    delegateChain("香蕉");
  }
}

或者:

delegate void EatDelegate(string food);

class DelegateMath
{
  static void Main()
  {
    EatDelegate delegateChain = null;
    delegateChain += delegate(string food){Console.WriteLine("张三"+food);};
    delegateChain += delegate(string food){Console.WriteLine("李四"+food);};
    delegateChain += delegate(string food){Console.WriteLine("王五"+food);};
    delegateChain("西瓜");
  }
}

 

 

**************************************    事件    *******************************


事件(event)
  我们可以把事件编程简单地分成两个部分:事件发生的类(书面上叫事件发生器)和事件接收处理的类。事件发生的类就是说在这个类中触发了一个事件,但这个类并不知道哪个个对象或方法将会加收到并处理它触发的事件。所需要的是在发送方和接收方之间存在一个媒介。这个媒介在.NET Framework中就是委托(delegate)。在事件接收处理的类中,我们需要有一个处理事件的方法。好了,我们就按照这个顺序来实现一个捕获键盘按键的程序,来一步一步说明如何编写事件应用程序。


1、首先创建一个自己的EventArgs类。
引自MSDN:
EventArgs是包含事件数据的类的基类,此类不包含事件数据,在事件引发时不向事件处理程序传递状态信息的事件会使用此类。如果事件处理程序需要状态信息,则应用程序必须从此类派生一个类来保存数据。
因为在我们键盘按键事件中要包含按键信息,所以要派生一个KeyEventArgs类,来保存按键信息,好让后面知道按了哪个键。


internal class KeyEventArgs : EventArgs
{
    private char keyChar;
    public KeyEventArgs( char keyChar ) : base()
    {
        this.keyChar = keyChar;
    }

    public char KeyChar
    {
        get
        {
            return keyChar;
        }
    }
}
2、再创建一个事件发生的类KeyInputMonitor,这个类用于监控键盘按键的输入并触发一个事件:
internal class KeyInputMonitor
{
    // 创建一个委托,返回类型为void,两个参数
    public delegate void KeyDown( object sender, KeyEventArgs e );
    // 将创建的委托和特定事件关联,在这里特定的事件为OnKeyDown
    public event KeyDown OnKeyDown;

    public void Run()
    {
        bool finished = false;
        do
        {
            Console.WriteLine( "Input a char" );
            string response = Console.ReadLine();

            char responseChar = ( response == "" ) ? ' ' : char.ToUpper( response[0] );
            switch( responseChar )
            {
                case 'X':
                    finished = true;
                    break;
                default:
                    // 得到按键信息的参数
                    KeyEventArgs keyEventArgs = new KeyEventArgs( responseChar );
                    // 触发事件
                    OnKeyDown( this, keyEventArgs );
                    break;
            }
        }while( !finished );
    }
}
这里注意OnKeyDown( this, KeyEventArgs );一句,这就是触发事件的语句,并将事件交由KeyDown这个委托来处理,委托指定事件处理方法去处理事件,这就是事件接收方的类的事情了。参数this是指触发事件的对象就是本身这个对象,keyEventArgs包含了按键信息。

3、最后创建一个事件接收方的类,这个类先产生一个委托实例,再把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件。然后提供一个方法回显按键信息。
internal class EventReceiver
{
    public EventReceiver( KeyInputMonitor monitor )
    {
        // 产生一个委托实例并添加到KeyInputMonitor产生的事件列表中
        monitor.OnKeyDown += new KeyInputMonitor.KeyDown( this.Echo );
    }
    private void Echo(object sender, KeyEventArgs e)
    {
        // 真正的事件处理函数
        Console.WriteLine( "Capture key: {0}", e.KeyChar );
    }
}
4、看一下如何调用
public class MainEntryPoint
{
    public static void Start()
    {
        // 实例化一个事件发送器
        KeyInputMonitor monitor = new KeyInputMonitor();
        // 实例化一个事件接收器
        EventReceiver eventReceiver = new EventReceiver( monitor );
        // 运行
        monitor.Run();
    }
}
总结:

C#中使用事件需要的步骤:
1.创建一个委托
2.将创建的委托与特定事件关联(.Net类库中的很多事件都是已经定制好的,所以他们也就有相应的一个委托,在编写关联事件处理程序--也就是当有事件发生时我们要执行的方法的时候我们需要和这个委托有相同的签名)
3.编写事件处理程序
4.利用编写的事件处理程序生成一个委托实例
5.把这个委托实例添加到产生事件对象的事件列表中去,这个过程又叫订阅事件

C#中事件产生和实现的流程:
1.定义A为产生事件的实例,a为A产生的一个事件
2.定义B为接收事件的实例,b为处理事件的方法
3.A由于用户(程序编写者或程序使用者)或者系统产生一个a事件(例如点击一个Button,产生一个Click事件)
4.A通过事件列表中的委托对象将这个事件通知给B
5.B接到一个事件通知(实际是B.b利用委托来实现事件的接收)
6.调用B.b方法完成事件处理

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值