自定义的事件管理器
自定義的事件管理器
周銀輝
?
大多數框架下都提供了事件管理器的,但不使用框架時為了讓事件發送者和事件接收者之間解耦,就可以如下寫個簡單的
public enum EventAdministratorEventTypes{ApplicationStartup, //在這里添加你需要的事件}public class EventAdministratorEventArgs : EventArgs{public object Arg{get;protected set;}public EventAdministratorEventArgs(object arg){Arg = arg;}}public class EventAdministrator{private static EventAdministrator instance;public static EventAdministrator Instance{get { return instance ?? (instance = new EventAdministrator()); }}/// <summary>/// 用于存儲event和它對于的回調函數列表的字典(event 和 callback是一對多的關系)/// </summary>private readonly Dictionary<EventAdministratorEventTypes, List<EventHandler<EventAdministratorEventArgs>>> eventAndCallbackBuffer = new Dictionary<EventAdministratorEventTypes, List<EventHandler<EventAdministratorEventArgs>>>();private void AddCallbackForEvent(EventAdministratorEventTypes eventName,EventHandler<EventAdministratorEventArgs> callback){if (!eventAndCallbackBuffer.ContainsKey(eventName)){eventAndCallbackBuffer.Add(eventName, new List<EventHandler<EventAdministratorEventArgs>>());}var callbackList = eventAndCallbackBuffer[eventName];//添加時判定不存在為了防止多次注冊后引起多次調用回調函數if (!callbackList.Contains(callback)){callbackList.Add(callback);}}/// <summary>/// 注冊為事件接受者/// </summary>/// <param name="eventName">事件名稱</param>/// <param name="callback">事件發生時的回調函數</param>public void Register(EventAdministratorEventTypes eventName, EventHandler<EventAdministratorEventArgs> callback){AddCallbackForEvent(eventName, callback);}/// <summary>/// 觸發一個事件/// </summary>/// <param name="sender">觸發者</param>/// <param name="eventName">事件名稱</param>/// <param name="args">觸發該事件時所帶的參數</param>public void Send(object sender, EventAdministratorEventTypes eventName, EventAdministratorEventArgs args){if (!eventAndCallbackBuffer.ContainsKey(eventName)){Logger.Warning("EventAdministrator:事件"+eventName+"被觸發,但沒有任何對象注冊該事件." +" 導致這個情況的原因可能有:代碼中沒有調用相應的Register函數來注冊該事件; 或者 事件的Send函數發生在Register之前");return;}var callbackList = eventAndCallbackBuffer[eventName];foreach (var callback in callbackList){callback.Invoke(sender, args);}}} }?
?
總結
- 上一篇: 梦到捉鸡是什么预兆
- 下一篇: 很想他为什么不会梦到他