美文网首页
二刷:事件

二刷:事件

作者: 山猪打不过家猪 | 来源:发表于2022-12-05 11:06 被阅读0次

1. 事件

image.png

1.1使用事件

  • 一个事件绑定两个时间处理器
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //创建时间拥有着
            Timer time1 = new Timer();
            //时间间隔1000ms
            time1.Interval = 1000;
            //事假响应者
            Boy boy = new Boy();
            Girl girl = new Girl();
            //绑定事件,事件订阅
            time1.Elapsed += boy.Action;
            time1.Elapsed += girl.Action;
            //开启事件
            time1.Start();
            Console.ReadLine();
        }
        public class Boy
        {
            //事假处理器
            internal void Action(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("jump!");
            }
        }

        public class Girl
        {
            internal void Action(object sender, ElapsedEventArgs e)
            {
                Console.WriteLine("sing");
            }
        }
    }

}
 

1.2一星事件应用

image.png
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows.Forms;

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Form form1 = new Form();
            Controllor controller_01 = new Controllor(form1);
            form1.ShowDialog();
        }
    }

    class Controllor
    {
        private Form form;

        public Controllor(Form form)
        {
            if (form != null)
            {
                this.form = form;
                //时间绑定:点击窗体,显示当前时间
                this.form.Click += this.FormClicked;
            }
        }

        private void FormClicked(object sender, EventArgs e)
        {
            //事件
            this.form.Text = DateTime.Now.ToString();
            
        }
    }
}
 

1.3二星事件

image.png

namespace DelegateDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            MyForm form = new MyForm();
            //给事件自己绑定事件
            form.Click += form.FormCliked;
            form.ShowDialog();
        }
    }

    class MyForm : Form
    {
        internal void FormCliked(object sender, EventArgs e)
        {
            this.Text = DateTime.Now.ToString();
        }
    }
}

1.4三星事件

image.png
  • 点击按钮,文本框里面的文字变化
namespace formDemo
{
    public partial class MyForm : Form
    {
        public MyForm()
        {
            InitializeComponent();
            this.button1.Click += button1_Click;      
        }
        private void button1_Click(object sender, EventArgs e)
        {
            this.textBox1.Text = "hello world!";
        }

1.5自定义事件

事件都是依赖于委托的

  • 完整格式
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace orderDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            //事件拥有这
            Customer customer = new Customer();
            //事件响应者
            Waiter waiter = new Waiter();
            //事件订阅
            customer.Order += waiter.Action;
            customer.Action();
            customer.PayTheBill();

        }
    }
    //传递事件参数的类
    public class OrderEventArgs:EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    public delegate void OrderEventHandler(Customer customer, OrderEventArgs e);

    public class Customer
    {
        private OrderEventHandler orderEventHandler;
        //声明事件
        public event OrderEventHandler Order { 
            add
            {
                this.orderEventHandler += value;
            }
            remove
            {
                this.orderEventHandler -= value;
            }
        }
        public double Bill { get; set; }
        public void PayTheBill()
        {
            Console.WriteLine("I wall Pay ${0} ", this.Bill);
        }

        public void WalkIn()
        {
            Console.WriteLine("walk into a restaurant");
        }
        public void SitDown()
        {
            Console.WriteLine("sit down");
        }
        public void Think()
        {
            for (int i = 1; i < 5; i++)
            {
                Console.WriteLine("Let me think..");
                Thread.Sleep(1000);
            }
            if (this.orderEventHandler !=null)
            {
                OrderEventArgs e = new OrderEventArgs();
                e.DishName = "chicken";
                e.Size = "large";

                this.orderEventHandler.Invoke(this, e);
            }
        }
        public void Action()
        {
            this.WalkIn();
            this.SitDown();
            this.Think();
            Console.ReadLine();
        }
    }
    //声明事件响应者
    public class Waiter
    {
        //事件处理器
        public void Action(Customer customer, OrderEventArgs e)
        {
            Console.WriteLine("I will serve you the dish - {0}", e.DishName);
            double price = 10;
            switch (e.Size)
            {
                case "small":
                    price = price * 0.5;
                    break;
                case "large":
                    price = price * 1.5;
                    break;
                default:
                    break;
            }
            customer.Bill += price;
        }
    }
}

相关文章

  • 二刷:事件

    1. 事件 1.1使用事件 一个事件绑定两个时间处理器 1.2一星事件应用 1.3二星事件 1.4三星事件 点击按...

  • 那年夏天 爱去了哪里 - 草稿

    这部电影我觉得很有必要二刷,一刷看剧情,二刷捋清事件脉络。 故事的主线是这样的 袁雎爸爸让袁雎杀了阿樱,但是袁雎没...

  • 2022-06-18

    6.18觉察日记 一、事件:备考刷题 二、应对:一致性 三、感受:紧张 四、想法: 临考前挤时间,熬夜刷题,想多考...

  • 二刷

    今天二刷头号玩家一部画面刺激情节紧凑经典重重 又不乏抒情的电影 放下手中的鼠标出去走走碧海蓝天吹吹风吧 让我

  • 二刷

    又看了一遍《妖孽神仙一念间》。太喜欢张迷经了。非常具有想象力和文字的掌控力。特别是他写了4篇文,每一篇的风格都不一...

  • 二刷👻

    啊啊啊啊啊啊啊啊啊啊 要死要死 纪言信 怎么这么会撩啊啊啊啊啊啊啊啊 我纪大大 受不了受不了

  • 二刷

    二刷了复联四,这次没有带家长,果然槽点都变了泪点~ 回到过去是一件多么需要勇气的事呢~ 有的人回到过去见到了自己想...

  • 二刷

    今天带着…或者说逼着爸妈一起和我再看了一遍《阿丽塔》。不同的是,我是二刷。 好看或者说喜欢的电影就是不会嫌烦,更何...

  • 二刷《》

    倾城之恋》是读过众多小说中唯一温暖而琐碎的爱情。可是这段情感的圆满又非常不幸地以一个大悲惨为前题为背景。那便是一个...

  • 二刷

    今天和朋友一起去看了《万里归途》,这次也是我第二次看这部电影了,可能是因为今天是周五的缘故,而且我们去的也很早,那...

网友评论

      本文标题:二刷:事件

      本文链接:https://www.haomeiwen.com/subject/gewefdtx.html