• C#委托和事件


          C#中的事件,即event,其采用的是事件驱动方式来使用,以前处理事件类似的事情往往采用等待机制,为了等待某事件的发生,需要不断地检测某些变量,而事件驱动程序等待事件时,不占用资源。

          事件从一方面来讲就是改变,就如button按钮一样,点击产生click事件,其click事件发生改变,就会通过委托调用相应的处理方法。

          委托,从某种程度上来说,委托好比一个通道,一个事件的发生,可以通过委托传递给不同的接受者,只要接受者订阅了此事件,就用权利在事件被触发时产生不同的处理方式。委托好比第三方,是独立于对象之外的东西。

     例:两个窗体实现文本框的互相传值:

    Form1:

    namespace WindowsForms

    {
        public delegate void myDelegate(string text);
        public partial class Form1 : Form
        {
            public event myDelegate deletForm1;
            public Form1()
            {
                InitializeComponent();
            }
            private void button1_Click(object sender, EventArgs e)
            {
                Form2 form = new Form2();
                form.deletForm2 += new myDelegate(this.TextMethod); //订阅事件
                this.deletForm1 += new myDelegate(form.TextMethod); //订阅事件
                form.Show();
              
            }

          //触发事件deletForm1

            public void button2_Click(object sender, EventArgs e)
            {
                if (deletForm1!= null)
                {            
                    deletForm1(textBox1.Text);
                }
            }

           //阅读Form2的deletForm2事件

                public void TextMethod(string text) {
                this.textBox1.Text = text;
            }
        }
    }

    Form2:

    namespace WindowsForms
    {
        public partial class Form2 : Form
        {
            public event myDelegate deletForm2;
            public Form2()
            {
                InitializeComponent();       
            }

     //触发事件deletForm1

            private void button1_Click(object sender, EventArgs e)
            {
                if(this.deletForm2!=null){
                    deletForm2(this.textBox1.Text);
                }
            }

     //阅读Form1的deletForm1事件

            public void TextMethod(string text)
            {
                this.textBox1.Text = text;
               
            } 

        }
    }

    EventAgrs是包含事件数据的类的积累,用于传递的事件的细节,在许多自定义事件中要定义自己的事件参数;

    自定义事件举例:

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Threading.Tasks;

    namespace EventMyself {   

      public delegate void MyDele(object sender, MyEventArgs e);    

    class Student     {

            public event MyDele OnLearn;//声明事件

            public virtual void DoEvent(MyEventArgs e)         {

                if (OnLearn != null)             {

                    OnLearn(this, e);//触发事件

                }

            }       

        }   

      public class MyEventArgs : EventArgs     {

            public int _book;

            public MyEventArgs(int book)         {

                this._book = book;

            }

        }

        class Teacher     {

            public void TeachBook(object sender, MyEventArgs e)//第一个参数其实这里传递的是对象的引用,实质只的是一个对象      

       {

                Console.WriteLine("I teach {0} books", e._book);     

        }

          public static void Main()         {

                Student stu = new Student();

                Teacher tea = new Teacher();

                MyEventArgs e = new MyEventArgs(3);

                stu.OnLearn += new MyDele(tea.TeachBook);//订阅事件

                stu.DoEvent(e);//触发事件         

                Console.Read();

            }

        }

    }

  • 相关阅读:
    课堂练习-电梯调度
    团队开发项目———来用————用户调研报告
    购书思想课堂作业4.14
    针对《来用》的NABC分析
    《梦断代码》读书笔记3
    《梦断代码》读书笔记2
    《大道至简》阅读笔记2
    《大道至简》阅读笔记1
    课堂练习之找出所有的“1”
    典型用户与场景分析
  • 原文地址:https://www.cnblogs.com/liuyuchneg-software/p/3387733.html
Copyright © 2020-2023  润新知