用winform程序来了解委托和事件

一、浅谈委托

如果有个过winform 和webform 程序开发的小伙伴一定有个这样的感觉吧,点击Button直接就执行了那个方法,到此他是怎么实现了的呢,大家有考虑过没有?

回到正题,什么是委托呢?委托,我们可以把看着是一个作为方法参数的一个方法,为什么呢,因为委托可以作为作为方法的参数,而给委托赋的值都是方法。下面我举一个例 

   /// <summary>
   /// 定义的一个打招呼的委托 /// </summary> /// <param name="name"></param> public delegate void GreetingDelegate(string name); /// <summary> /// 中国人打招呼的类 /// </summary> public class Chinese { public void ChineseGreeting(string name) { Console.Write("早上好--" + name); } } public class English { public void EnglishGreeting(string name) { Console.Write("Good Morning--" + name); } } class Program { static void Main(string[] args) { Chinese people1 = new Chinese(); English people2 = new English(); Greeting("欢大少", people1.ChineseGreeting); Console.Write("\n"); Greeting("Master", people2.EnglishGreeting); Console.ReadKey(); } private static void Greeting(string name,GreetingDelegate MakingGreeting ) { MakingGreeting(name); } }

运行结果 

 

当然,实质上委托是一个特殊的类,知识有限,只能讲到这儿了,

事件是什么呢,其实事件就是用event修饰后的委托,

   class Program
    {
        static void Main(string[] args)
        {
            Publishser pb = new Publishser();
            Subscriber sb = new Subscriber();
            //pb.NumberChange += sb.OnNumberChange;
            pb.NumberChange = sb.OnNumberChange;
            pb.DoSomething();
            pb.NumberChange(100);
            Console.ReadLine();
        }
    }

    /// <summary>
    /// 定义委托
    /// </summary>
    public  delegate void NumberChangeEventHandler(int count);

    public class Publishser
    {
        private int count;
        public NumberChangeEventHandler NumberChange;
        //public event NumberChangeEventHandler NumberChange;
        public void DoSomething()
        {
            if(NumberChange!=null)
            {
                count++;
                NumberChange(count);
            }
        }
    }

    public class Subscriber
    {
        public void OnNumberChange(int count) 
        {
            Console.WriteLine("Subscriber notified count ={0}", count);
        }
    }

上面的代码如果是定义普通的委托能够正常运行,但如果换成事件来的话变异不能通过,为什么呢?因为为了更好的封装性,实际是事件是一个私有变量,外部不是访问,如果用委托变量,外部能够调用委托,影响封装性,就像上面代码

pb.NumberChange(100);,如果定义为事件,那么NumberChange只能通过内部方法DoSomething来调用。

 事件只能通过+=来进行赋值,如果你反编译你可以看到事件里有两个方法 Add() 和Remove(),分别对应的是+=和-=。

二、winform程序click事件的原理

在form的构造函数中,我们能看到下面的代码

     /// <summary>
        /// 设计器支持所需的方法 - 不要
        /// 使用代码编辑器修改此方法的内容。
        /// </summary>
        private void InitializeComponent()
        {
            this.button1 = new System.Windows.Forms.Button();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(391, 117);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(75, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "button1";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(808, 400);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "F1";
            this.ResumeLayout(false);

        }

 

我们发现对事件的添加 this.button1.Click += new System.EventHandler(this.button1_Click);

这个地方我们只是添加了事件,但这个事件是怎么触发的呢?我们进行调试

 

 

 

 button的click的触发经过如下过程

 

三、模拟winform事件

 

 

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace MonitorEvent
{
    /// <summary>
    /// 定义委托
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    public delegate void MonitorEventHandler(object sender, EventArgs e);

    /// <summary>
    /// 时间数据类
    /// </summary>
    public class MsgEventArgs : EventArgs
    {
        /// <summary>
        /// 修改时间
        /// </summary>
        public DateTime ChanggeTime { get; set; }
        
        /// <summary>
        /// 提示信息
        /// </summary>
        public string ToSend { get; set; }
        public MsgEventArgs(DateTime changgeTime, string toSend)
        {
            this.ChanggeTime = changgeTime;
            this.ToSend = toSend;
        }
    }
    /// <summary>
    /// 事件订阅者
    /// </summary>
    public class MonitorText
    {
        public string name = "文本文档";
        //定义监控文本事件
        public event MonitorEventHandler MonitorEvent;
        //上次文件更新时间用于判断文件是否修改过
        private DateTime _lastWriteTime = File.GetLastWriteTime(@"C:\Users\Administrator\Desktop\1.txt");
        public MonitorText()
        {

        }
        // 文件更新调用
        protected virtual void OnTextChange(MsgEventArgs e)
        {
            if (MonitorEvent != null)
            {
                //不为空,处理事件
                MonitorEvent(this, e);
            }
        }

        //事件监听的方法
        public void BeginMonitor()
        {
            DateTime bCurrentTime;

            while (true)
            {
                bCurrentTime = File.GetLastWriteTime(@"C:\Users\Administrator\Desktop\1.txt");
                if (bCurrentTime != _lastWriteTime)
                {
                    _lastWriteTime = bCurrentTime;
                    MsgEventArgs msg = new MsgEventArgs(bCurrentTime, "文本改变了");
                    OnTextChange(msg);
                }
                //0.1秒监控一次
                Thread.Sleep(100);
            }
        }

    }

    /// <summary>
    /// 事件监听者
    /// </summary>
    public class Administrator
    {
        //管理员事件处理方法
        public void OnTextChange(object Sender, EventArgs e)
        {
            MonitorText monitorText = (MonitorText)Sender;
            Console.WriteLine("尊敬的管理员:" + DateTime.Now.ToString() + ": " + monitorText.name + "发生改变.");
        }
    }

    class Program
    {
        static MonitorText MonitorTextEventSource;
        static void Main(string[] args)
        {
            MonitorTextEventSource = new MonitorText();
            //1. 启动后台线程添加监视事件
            var thrd = new Thread(MonitorTextEventSource.BeginMonitor);
            thrd.IsBackground = true;
            thrd.Start();
            //2实例化管理员类
            Administrator ad = new Administrator();
            //4订阅事件
            MonitorTextEventSource.MonitorEvent += ad.OnTextChange;
            Console.ReadLine();
        }
    }
}

四、申明

文中的观点多来源与张子阳的.net之美和ATtuing的博客,希望大家共同学习

 

posted @ 2016-12-27 13:44  欢大少  阅读(6657)  评论(3编辑  收藏  举报