精易论坛

标题: 易转C# - 4 - 自定义事件 [打印本页]

作者: 陽陽陽    时间: 2023-6-5 12:34
标题: 易转C# - 4 - 自定义事件
事件是什么:


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

事件的完整声明:


复杂例子:
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里面

作者: 神女软件定制    时间: 2023-6-5 13:56
我理解:事件是在当前类外受限的委托




欢迎光临 精易论坛 (https://125.confly.eu.org/) Powered by Discuz! X3.4