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

C#委托,事件。

2014年02月17日 ⁄ 综合 ⁄ 共 3006字 ⁄ 字号 评论关闭

1  委托

    委托是函数的引用!其主要用于事件。

    委托使用delegate声明,注意,匿名函数也是使用delegate。

    委托的使用方法  1)定义委托     2)声明委托     3)实例化委托(为委托添加处理函数)     4)使用委托。

namespace Test
{
    class Program
    {
        delegate void dele_test();        //定义委托
        static void Main(string[] args)
        {
            dele_test dt;
            //dt = new dele_test(Write);  //实例化委托的方法1
            dt = Write;                   //实例化委托的方法2

            dt();                         //使用委托
            Console.Read();
        }

        public static void Write()
        {

            Console.Write("这就是委托");
        }
    }
}

结果:这就是委托

 

2 计时器事件

   什么是事件?事件和异常相似,在特定的情况下发生!不同的是,异常我们需要捕获try{}   catch(){},而事件需要订阅!不多说了,为了复习一下委托,先说一个计时器事件,计时器事件就是在特定的时间发生的事件。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;

namespace Test
{
    class Program
    {
        public static int i = 0;
        //定义一个计时器对象,注意引用using System.Timers;名称空间

        static void Main(string[] args)
        {
            Timer t1 = new Timer(1000);
            Timer t2 = new Timer(1500);

            //为计时器事件Elapsed添加响应的委托。
            t1.Elapsed += new ElapsedEventHandler(Write1);//通过委托的来添加响应函数
            t2.Elapsed += Write1;                         //直接通过函数名称添加。    
 

            t1.Start();  // 启动计时器。
            t2.Start();

            Console.Read();
        }

        public static void Write1(Object o, ElapsedEventArgs e)
        {
            if (((Timer)o).Interval == 1000)
            {
                Console.WriteLine("1");
            }
            else
            {
                Console.WriteLine("2");
            }
        }

    }
}

结果:1

           2

           1

           1

          。。。

 

Timer的计时器相应事件是Elapsed,我们需要人为的设置事件响应程序。

为事件添加响应程序的方法有两种:

            1)使用委托  eg:

                  t1.Elapsed += new ElapsedEventHandler(Write1);

            2)直接使用函数地址(函数名称)

                  t2.Elapsed += Write1;

 

3:自定义事件(关键字event)

      之前说过,委托的主要功能体现在事件上!事件也不仅仅是“计时器”。

      自定义事件步骤: 1) 定义事件    2)为事件添加委托     3)引发事件

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;

namespace Test
{
    public class Test
    {
        public class EventTest
        {

            public delegate void DeleTest();
            public event DeleTest eventTest;//定义事件
            public EventTest()
            {
                DeleTest dt = new DeleTest(Write1);
                eventTest += new DeleTest(Write1);//为事件添加委托 也可以 eventTest +=Write1;
              }
            public void QiDongEvent() //引发事件。
            {
                eventTest();          //就好像返回类型和参数是有委托指定的函数一样!
            }
        }
        public static int i = 10;
        static void Main(string[] args)
        {
            EventTest et = new EventTest();

            while (i > 0)                //当i>0的时候,激活一次事件。
            {
                et.QiDongEvent();        
                Thread.Sleep(1000);
            }

            Console.Read();
        }

        public static void Write1()
        {

            i--;
            Console.Write("-" + i);

        }

    }
}

结果:
 

 

4 向事件传递参数

   向事件传递参数,需要在被委托指定的函数里面添加两个参数:Object对象,EventArgs抽象类的子类对象。

   Object对象代表了调用事件对象的引用!

   EventArgs抽象类的子类封装了要传递进去的内容。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Threading;

namespace Test
{
    public class Test
    {
        class Args : EventArgs          //该类封装了要传递的信息。
        {
            public string name;
            public Args(string str)
            {

                name = str;
            }

        }
        public class EventTest
        {

            public delegate void DeleTest(Object o, EventArgs e);
            public event DeleTest eventTest;

            public EventTest()
            {
                DeleTest dt = new DeleTest(Write1);
                // eventTest += new DeleTest(Write1);
                eventTest += Write1;
            }
            public void QiDongEvent(string str) //引发事件。
            {
                eventTest(this, new Args(str));          //就好像返回类型和参数是那个被委托指定的函数一样!
            }
        }
        public static int i = 10;
        static void Main(string[] args)
        {
            EventTest et = new EventTest();

            while (i > 0)                //当i>0的时候,激活一次事件。
            {
                et.QiDongEvent("---" + DateTime.Now.Second.ToString());
                Thread.Sleep(1000);
            }

            Console.Read();
        }

        public static void Write1(Object o, EventArgs e)
        {

            i--;
            Console.WriteLine(o.ToString() + "-" + i + ((Args)e).name);

        }

    }
}

结果:

           

                                         

 

   

 

抱歉!评论已关闭.