C#之委託如此簡單

  • 2019 年 11 月 10 日
  • 筆記

     近期和幾位做嵌入式開發的朋友閑聊過程中,一位朋友抱怨到:這C#太難用了,我想在N個窗體(或者是N個用戶組件之間)傳遞值都搞不定,非得要定義一個全局變數來存儲,然後用定時器來刷新值,太Low了。我急切的回答道:這很簡單,不就是委託的事嘛。那你來一個示例啊:朋友道。此為這篇部落格的起因,所以此篇部落格對於有c#開發經驗的夥伴們那是小菜一喋。

一、對委託的理解

     委託:同一個功能,可以根據不同的場景委託給不同的方法具體執行; 舉個栗子:某位美食愛好妹子,通常自己做美食;找到男票後,就男票做美食;換男票後,就第二任男票做美食。我們把這例子委託抽象化:

    定義一個委託功能:做美食;規範及流程:輸入”食材“,通過”做美食“的委託,輸出”美食“。

    委託實現之自己做:妹子自己做美食

    委託實現之一號男票做:一號男票做美食

    委託實現之二號男票做:二號男票做美食

    做美食這項功能,被妹子在不同的時間段分配給了不同的對象,雖然妹子,男一,男二做的美食都很好吃,但味道肯定有區別。這就是委託生活化的示例,各位看觀了解否(偷笑)。

二、程式碼實現

    上面的示例如何用程式碼實現,這裡就不展示了(真的很簡單)。下面我們換一個稍有難度和實際應用的示例,需求說明:主窗體顯示一個列表,子窗體增加數據(不關閉子窗體的情況下),主窗體列表自動更新,且第二個子窗體打開後,窗體內的列表也要同時更新。

    UI設計:一個主窗體,兩個子窗體(A窗體:增加數據,B窗體:顯示數據),一個用戶組件(列表顯示內容)

2.1 EventBus實現

程式碼如下:

public class EventBus<T>      {          private List<T> list = new List<T>();            public event EventHandler<EventBusArg<List<T>>> EventNotice;          public delegate void DelegateItemInfoEvent(List<T> items);            public void Add(T item)          {              this.list.Add(item);              this.TriggerEventNotice();          }            public void Remove(T item)          {              this.list.Remove(item);              this.TriggerEventNotice();          }            public List<T> GetAll()          {              return this.list;          }            private void TriggerEventNotice()          {              if (this.EventNotice != null)              {                  this.EventNotice.Invoke(this, new EventBusArg<List<T>>()                  {                      Data = this.GetAll()                  });              }          }      }        public class EventBusArg<T> : EventArgs      {          public T Data { get; set; }      }

重點:

1. 定義了一個委託類型:DelegateItemInfoEvent(List<T> items)

2. 定義了一個事件對象:EventHandler<EventBusArg<List<T>>>

3. 事件對象的參數必須繼承EventArgs對象

4. 事件依賴委託

2.2 主窗體

程式碼如下:

 

        private EventBus<ItemInfo> eventBus = new EventBus<ItemInfo>();          private EventBus<ItemInfo>.DelegateItemInfoEvent FunItem;          public Form1()          {              InitializeComponent();              this.eventBus.EventNotice += EventBus_EventNotice;          }            private void EventBus_EventNotice(object sender, EventBusArg<List<ItemInfo>> e)          {              if (this.ucList1.InvokeRequired)              {                  FunItem = new EventBus<ItemInfo>.DelegateItemInfoEvent(RefreshItem);                  this.ucList1.Invoke(FunItem, e.Data);              }              else              {                  this.RefreshItem(e.Data);              }          }            private void RefreshItem(List<ItemInfo> item)          {              var ls = this.eventBus.GetAll();              this.ucList1.LoadData(ls);          }

重點:

1. 捕獲事件:this.eventBus.EventNotice += EventBus_EventNotice;

2. 事件處理方法中,需要判斷是否為UI執行緒引發,如果不是,則需要委託來進行切換執行緒,程式碼見:private void EventBus_EventNotice(object sender, EventBusArg<List<ItemInfo>> e) 方法

3. 其中FunItem是委託類型的變數,其最終的實現為RefreshItem方法

2.3 A窗體:增加數據

程式碼如下:

private EventBus<ItemInfo> eventBus;          public Form2(EventBus<ItemInfo> eventBus)          {              this.eventBus = eventBus;              InitializeComponent();          }            private void button1_Click(object sender, EventArgs e)          {              //在UI執行緒              this.eventBus.Add(new ItemInfo()              {                  Title = textBox1.Text,                  Val = Int32.Parse(textBox2.Text)              });          }            private void button2_Click(object sender, EventArgs e)          {              //跨執行緒              Task.Factory.StartNew(() =>              {                  for(var i=10; i < 15; i++)                  {                      this.eventBus.Add(new ItemInfo()                      {                          Title = i.ToString() + "-Title",                          Val = i                      });                  }              });          }

重點:

1. 傳入了EventBus對象的實例,此實例與主介面的EventBus實例為同一個【這點很重要,發布和訂閱的事件必須在同一實例上】

2. button2_Click事件展示的是跨執行緒事件,執行此程式碼,主介面的刷新會走委託

2.4 B窗體:訂閱列表顯示

程式碼如下:

private EventBus<ItemInfo> eventBus;          public Form3(EventBus<ItemInfo> eventBus)          {              this.eventBus = eventBus;              InitializeComponent();              this.eventBus.EventNotice += EventBus_EventNotice;          }            private void EventBus_EventNotice(object sender, EventBusArg<List<ItemInfo>> e)          {              if (this.ucList1.InvokeRequired)              {                  var FunItem = new EventBus<ItemInfo>.DelegateItemInfoEvent(RefreshItem);                  this.ucList1.Invoke(FunItem, e.Data);              }              else              {                  this.RefreshItem(e.Data);              }          }            private void RefreshItem(List<ItemInfo> item)          {              var ls = this.eventBus.GetAll();              this.ucList1.LoadData(ls);          }            private void Form3_FormClosing(object sender, FormClosingEventArgs e)          {              this.eventBus.EventNotice -= EventBus_EventNotice;          }

重點:

1. 事件的訂閱與取消訂閱,一般情況下可以在關閉窗體時取消訂閱

三、回顧

1. 事件依賴委託,事件可以訂閱和取消訂閱,其訂閱就是為事件增加委託。

2. 委託的本質還是方法(或者說是函數),只不過方法變成了一個變數,可以在運行時動態改變