1. 程式人生 > >委託和事件 簡要筆記

委託和事件 簡要筆記

筆記來源於劉鐵錳老師的視訊講解後自己的記錄梳理,推薦看看劉鐵錳老師的相關教學視訊 《c#系列》《wpf系列》《MVVM系列》《對答如劉系列》 還有一個《C# WPF .NET 資料繫結和資料庫開發基礎(第四季) 》這個是傳智播客的視訊

上面的視訊在b站上都有,可以自行搜尋

關於委託和事件的一些語法啊,宣告方式啊,相關觸發事件的邏輯啊,大家可以看看相關原始碼 https://referencesource.microsoft.com 這個網站可以看相關原始碼,沒事可以翻翻。

1,委託 關於Action和Fun<>就不說了 一個delegate簡單例子

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    public delegate void Test(int a, int b);
    class Program
    {
        static void Main(string[] args)
        {
            Test test = null;//在這裡聲明瞭一個委託型別的變數
            Apple apple = new Apple();
            test += apple.add;//使用 += 來將apple.add函式放在這個委託變數中
            test(1, 2);//通過委託變數來簡介呼叫apple.add函式
        }
    }
    class Apple
    {
        public void add(int a, int b)
        {
            Console.WriteLine(a+b);
        }
    }
}

就這。。。。宣告委託類時注意和相對應的函式一樣一樣的。。。

關於事件

在這裡插入圖片描述 先作下個人表達理解: 事件的定義其實就是微軟設計的一種C#語法的規範,這個語法規則封裝了(或者說關聯了)Windows訊息,委託 其作用:清晰理解、安全操作 而激發事件------其實就是激發委託來間接的呼叫響應函式

關於事件,我們以上圖模式來說明(我意思是關於事件也有其他模式,只是物件位置的關係)

1,首先物件A中有個事件(關於事件的定義,要不你們還是看看視訊中的說法吧。。。我反正不太願意聽這些概覽。。。怕聽不好。。) 2,將物件B和物件A進行關聯(物件B來訂閱物件A的事件) 3,然後這個事件需要有邏輯來觸發(當你實際編寫關於自定義事件的程式時,你就會發現這裡其實就是通過委託來呼叫響應函式) 在3中這個響應函式其實就和委託關聯了,同時事件也和委託關聯了 (委託規定了響應函式的格式),在這個格式中(即形參中有物件A和相關訊息) 4,然後響應函式就開始執行了。。。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Deployment.Internal;
using System.Dynamic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading;

namespace ConsoleApplication3
{
   
    public delegate void OrderEventHandler(Customer senderCustomer, OrderEventArgs eventArgs);
    //1,如此命名讓別人一看就知道 這個委託是用來宣告事件的 反過來說:我們要自定義一個事件就需要先宣告一個委託
    //2,同時這個委託也會來約束事件處理器
    //3,這個委託的例項時用來儲存事件處理器

    class Program
    {
        static void Main(string[] args)
        {
            Customer customer = new Customer();
            Waiter waiter = new Waiter();
            customer.Order += waiter.action;
            customer.action();
   
            customer.PayTheBill();

            Console.ReadLine();
        }
    }

    public class Customer
    {

        private OrderEventHandler orderEventHandler;

        public event OrderEventHandler Order//宣告一個事件!
        {
            add
            {
                this.orderEventHandler += value;
            }
            remove 
            {
                this.orderEventHandler -= value;
            }
        }
        
        public void WalkIn()
        {
        Console.WriteLine("walkin....");
        }

        public void SitDown()
        {
        Console.WriteLine("sitdown....");
        }

        public void Think()
        {

            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("thing...");
                Thread.Sleep(1000);
            }
            
           //經過前面一堆有的沒的事情後,開始觸發事件了。。。
            if (this.orderEventHandler != null)//沒人訂閱這個事件,沒有事件處理器來處理這個事件
            {
                OrderEventArgs e= new OrderEventArgs();
                e.DishName ="rou";
                e.Size = "large";
                this.orderEventHandler(this, e);//這裡不就是通過委託來間接的呼叫響應函式嗎,所以這裡與其說想辦法來觸發事件,不如說想辦法來通過委託變數來間接的呼叫相應函式
            }
            /////////////////////////////
        }

        public void action()
        {
            Console.ReadLine();
            this.WalkIn();
            this.SitDown();
            this.Think();
        }

        public double Bill { get; set; }//要付的錢

        public void PayTheBill()
        {
            Console.WriteLine("I will pay ${0}" ,Bill);
        }
    }

    public class OrderEventArgs:EventArgs
    {
        public string DishName { get; set; }
        public string Size { get; set; }
    }

    class Waiter
    {
        public void action(Customer senderCustomer, OrderEventArgs eventArgs)
        {
            // Customer sendercustomer = sender as Customer;
            //OrderEventArgs eventargs = e as OrderEventArgs;
            Console.WriteLine("I will server you the dish- {0}", eventArgs.DishName);
            double price = 10;

            switch (eventArgs.Size)
            {
                case "small":
                    price = 5;
                    break;

                case "large":
                    price = 15;
                    break;
                default:
                    break;
            }
            senderCustomer.Bill += price;
        }      
    }
}

`
大家去看看連結中的視訊吧,個人講不好。。。作為記錄用