开启辅助访问 切换到宽版

精易论坛

 找回密码
 注册

QQ登录

只需一步,快速开始

用微信号发送消息登录论坛

新人指南 邀请好友注册 - 我关注人的新帖 教你赚取精币 - 每日签到


求职/招聘- 论坛接单- 开发者大厅

论坛版规 总版规 - 建议/投诉 - 应聘版主 - 精华帖总集 积分说明 - 禁言标准 - 有奖举报

查看: 420|回复: 1
收起左侧

[C#图文教程] 易转C# - 4 - 自定义事件

[复制链接]
结帖率:95% (84/88)
发表于 2023-6-5 12:34:36 | 显示全部楼层 |阅读模式   美国
事件是什么:


可以理解成 事件 = 委托
但是又绝对不等于,事件可以防止借刀杀人。

事件的完整声明:


复杂例子:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;

namespace Test2
{
    //public delegate double Calc(double x, double y);
    public partial class Form1 : Form
    {

        public Form1()
        {
            InitializeComponent();
        }
        public void nothing()
        {

            //ADic.Add("w", 2);
            //Console.WriteLine(ADic["w"]);
            //Console.WriteLine(ADic.ContainsKey("w"));
            //Bitmap img = new Bitmap("C:\\ESouceCode\\Eweb-main\\易网页1.0\\photos\\宣传AND图片\\陽.png");
            //pictureBox1.Image = img;
            //string test = "qwerty112233445566";
            //Console.WriteLine(test.Substring(0, 1));
            //Console.WriteLine(test.Replace("3", "ddd"));
            //Console.WriteLine(test);
            //int[] intarray = new int[] { 1, 2, 3, 4, 5, 6, 7, 8 };
            //IEnumerator theenumerator = intarray.GetEnumerator();
            //while (theenumerator.MoveNext())
            //{
            //    Console.WriteLine(theenumerator.Current);
            //}
            //int[] arr = new int[] { 1, 2, 3, 4, 5 };
            //foreach (int i in arr)
            //{
            //    Console.WriteLine(arr[i - 1]);
            //}
            //List<int> thelist = new List<int>();
            //thelist.Insert(0, 1);
            //thelist.Insert(0, 2);
            //thelist.Insert(0, 3);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 4);
            //thelist.Insert(0, 5);
            //thelist.RemoveAt(0);
            //throw new Exception("e");
            //List<int> rem = new List<int> { 4, 2 };
            //thelist.RemoveAll(member => rem.Contains(member));
            //thelist.Remove(4);
            //foreach (int i in thelist)
            //{
            //    Console.WriteLine(i);
            //}
            //Console.WriteLine(Math.PI);
            //test t1 = new test();
            //test t2 = new test();
            //t1.SetI(2);
            //t1.PrintI();
            //t2.SetI(9);
            //t2.PrintI();
            //t1.PrintI();
            //int i = 0;
            //ecde(ref i);

            //try
            //{
            //    MessageBox.Show("2");
            //}
            //catch(OverflowException)
            //{
            //    MessageBox.Show("3");
            //}
            //catch (AccessViolationException)
            //{

            //}
        }
        public void nothing2()
        {
            //button2.Click += Button2_ClickTest;
            //Func<int> testFunc = new Func<int>(testThread);
            //AsyncCallback testCallBack_AC = new AsyncCallback(testCallBack);

            //testFunc.BeginInvoke(testCallBack_AC, null);

            //Calculator ACalculator = new Calculator();
            //Calc calc1 = new Calc(ACalculator.Add);
            //ProductFactory TheproductFactory = new ProductFactory();
            //WarpFactroy ThewarpFactroy = new WarpFactroy();

            //Func<Product> fun1 = new Func<Product>(TheproductFactory.MakePizza);
            //Func<Product> fun2 = new Func<Product>(TheproductFactory.MakeToyCar);

            //Box box1 = ThewarpFactroy.WarpPoroduct(fun1);
            //Box box2 = ThewarpFactroy.WarpPoroduct(fun2);

            //Console.WriteLine(box1.Product.Name);
            //Console.WriteLine(box2.Product.Name);
            //Action<string, int> testAct = tesActFu;
            //testAct.Invoke("qqq", 22);
            //this.button2.Click += (object sender_, EventArgs e_) => {
            //    // do something here
            //};
        }








        private void Form1_Load(object sender, EventArgs e)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();

            customer.Order += waiter.Action;
            customer.Think();
        }


        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 will pay {0}", this.Bill);
            }

            public void Think()
            {
                if (this.orderEventHandler != null)
                {
                    OrderEventArgs e = new OrderEventArgs();
                    e.DishName = "Kongpao Chiken";
                    this.orderEventHandler.Invoke(this, e);
                }
            }
        }


        public class Waiter
        {
            public void Action(Customer customer, OrderEventArgs e)
            {
                Console.WriteLine("I will serve you the dish = {0}", e.DishName);

            }
        }




        class Product
        {
            public string Name { get; set; }
        }
        class Box
        {
            public Product Product { get; set; }
        }
        class WarpFactroy
        {
            public Box WarpPoroduct(Func<Product> getProduct)
            {
                Box box = new Box();
                Product product = getProduct.Invoke();
                box.Product = product;
                return box;
            }
        }
        class ProductFactory
        {
            public Product MakePizza()
            {
                Product product = new Product();
                product.Name = "Pizza";
                return product;
            }
            public Product MakeToyCar()
            {
                Product product = new Product();
                product.Name = "Toy Car";
                return product;
            }
        }


    }
}



简单例子:

using System;

namespace ConsoleApp
{
    public delegate void MyEventHandler(string message);

    public class Publisher
    {
        public event MyEventHandler MyEvent;

        public void DoSomething()
        {
            // 触发事件
            if (MyEvent != null)
            {
                MyEvent("Event triggered");
            }
        }
    }

    public class Subscriber
    {
        public void OnMyEvent(string message)
        {
            Console.WriteLine(message);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Publisher publisher = new Publisher();
            Subscriber subscriber = new Subscriber();

            // 订阅事件
            publisher.MyEvent += subscriber.OnMyEvent;

            // 执行操作,触发事件
            publisher.DoSomething();

        }
    }
}




事件的简化声明:

实在不愿意看了,跳过,有大佬给个例子也行。



类:

类是一种数据类型,最初出现在C with class里面

结帖率:100% (9/9)

签到天数: 11 天

发表于 2023-6-5 13:56:27 | 显示全部楼层   重庆市重庆市
我理解:事件是在当前类外受限的委托

评分

参与人数 1好评 +1 精币 +3 收起 理由
陽陽陽 + 1 + 3 现在听不懂但是大感震撼

查看全部评分

回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则 致发广告者

发布主题 收藏帖子 返回列表

sitemap| 易语言源码| 易语言教程| 易语言论坛| 易语言模块| 手机版| 广告投放| 精易论坛
拒绝任何人以任何形式在本论坛发表与中华人民共和国法律相抵触的言论,本站内容均为会员发表,并不代表精易立场!
论坛帖子内容仅用于技术交流学习和研究的目的,严禁用于非法目的,否则造成一切后果自负!如帖子内容侵害到你的权益,请联系我们!
防范网络诈骗,远离网络犯罪 违法和不良信息举报电话0663-3422125,QQ: 793400750,邮箱:wp@125.la
Powered by Discuz! X3.4 揭阳市揭东区精易科技有限公司 ( 粤ICP备12094385号-1) 粤公网安备 44522102000125 增值电信业务经营许可证 粤B2-20192173

快速回复 返回顶部 返回列表