12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- using System.Collections.Generic;
- public class MsgHandler
- {
- public delegate void DelMsgHandler(ModelData msg);
- private static Dictionary<string, DelMsgHandler> mDicMsgs = new Dictionary<string, DelMsgHandler>();
-
-
-
-
-
- public static void AddListener(string msgType, DelMsgHandler handler)
- {
-
- if (mDicMsgs == null) mDicMsgs = new Dictionary<string, DelMsgHandler>();
- if (!mDicMsgs.ContainsKey(msgType)) mDicMsgs.Add(msgType, null);
-
- mDicMsgs[msgType] += handler;
- }
-
-
-
-
-
- public static void RemoveListener(string msgType, DelMsgHandler handler)
- {
- if (mDicMsgs != null && mDicMsgs.ContainsKey(msgType)) mDicMsgs[msgType] -= handler;
- }
-
-
-
- public static void ClearAllListeners()
- {
- if (mDicMsgs != null) mDicMsgs.Clear();
- }
-
-
-
-
-
- public static void SendMsg(string msgType, ModelData msg)
- {
- DelMsgHandler handler;
- if (mDicMsgs != null && mDicMsgs.TryGetValue(msgType, out handler))
- {
- if (handler != null)
- handler(msg);
- }
- }
-
-
-
-
- public static void AllSendMsg(ModelData msg)
- {
- foreach (var item in mDicMsgs)
- {
- item.Value(msg);
- }
- }
- }
- public class Msg
- {
- public string Key { get; private set; }
- public object Value { get; private set; }
- public Msg(string key, object value)
- {
- this.Key = key;
- this.Value = value;
- }
- }
|