190 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
		
		
			
		
	
	
			190 lines
		
	
	
		
			4.8 KiB
		
	
	
	
		
			C#
		
	
	
| 
								 | 
							
								using System;
							 | 
						||
| 
								 | 
							
								using System.Collections.Generic;
							 | 
						||
| 
								 | 
							
								using System.Linq;
							 | 
						||
| 
								 | 
							
								using System.Text;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace GuruClient
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    public interface IEventData
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        string Name { get; }
							 | 
						||
| 
								 | 
							
								        object Sender { get; }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public class BaseIEventData : IEventData
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        public string Name
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            get
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                return GetType().FullName;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        public object Sender
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            get; private set;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        public BaseIEventData()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        public BaseIEventData(object sender)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            Sender = sender;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public class EventData : BaseIEventData
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        private readonly string _data;
							 | 
						||
| 
								 | 
							
								        public string Data { get { return _data; } }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        public EventData(string data)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _data = data;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    public delegate void MyEventHandler(IEventData data);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /// <summary>
							 | 
						||
| 
								 | 
							
								    /// 全局事件管理器
							 | 
						||
| 
								 | 
							
								    /// </summary>
							 | 
						||
| 
								 | 
							
								    public class EventManager
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        #region 单例
							 | 
						||
| 
								 | 
							
								        private static readonly EventManager _instance = new EventManager();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        internal static void AddListenner(string uI_SignIn_SignHandler1, string uI_SignIn_SignHandler2)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            throw new NotImplementedException();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								        #endregion
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        #region 静态公共接口
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 为指定事件注册一个处理函数
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="handler"></param>
							 | 
						||
| 
								 | 
							
								        public static void AddListenner(string eventName, MyEventHandler handler)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _instance.AddEventHandler(eventName, handler);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 取消指定事件的指定处理函数
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="handler"></param>
							 | 
						||
| 
								 | 
							
								        public static void RemoveListenner(string eventName, MyEventHandler handler)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _instance.RemoveEventHandler(eventName, handler);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 取消指定事件的所有处理函数
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        public static void ClearEvent(string eventName)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _instance.ClearEventHandler(eventName);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 取消所有事件监听
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        public static void Clear()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _instance.ClearAllEvent();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 激发一个事件
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="data"></param>
							 | 
						||
| 
								 | 
							
								        public static void Dispatch(string eventName, IEventData data)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _instance.FireEvent(eventName, data);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        public static void Dispatch(string eventName)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            Dispatch(eventName, null);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        #endregion
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        private readonly Dictionary<string, MyEventHandler> _handlerDict = new Dictionary<string, MyEventHandler>();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 为指定事件注册一个处理函数
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="handler"></param>
							 | 
						||
| 
								 | 
							
								        private void AddEventHandler(string eventName, MyEventHandler handler)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if (handler == null)
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                return;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            if (_handlerDict.ContainsKey(eventName))
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _handlerDict[eventName] += handler;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								            else
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _handlerDict.Add(eventName, handler);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 取消指定事件的指定处理函数
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="handler"></param>
							 | 
						||
| 
								 | 
							
								        private void RemoveEventHandler(string eventName, MyEventHandler handler)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if (_handlerDict.ContainsKey(eventName))
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _handlerDict[eventName] -= handler;
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        private void ClearEventHandler(string eventName)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if (_handlerDict.ContainsKey(eventName))
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                _handlerDict.Remove(eventName);
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        private void ClearAllEvent()
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            _handlerDict.Clear();
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        /// <summary>
							 | 
						||
| 
								 | 
							
								        /// 激发一个事件
							 | 
						||
| 
								 | 
							
								        /// </summary>
							 | 
						||
| 
								 | 
							
								        /// <param name="eventName"></param>
							 | 
						||
| 
								 | 
							
								        /// <param name="data"></param>
							 | 
						||
| 
								 | 
							
								        private void FireEvent(string eventName, IEventData data)
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if (_handlerDict.ContainsKey(eventName))
							 | 
						||
| 
								 | 
							
								            {
							 | 
						||
| 
								 | 
							
								                var handler = _handlerDict[eventName];
							 | 
						||
| 
								 | 
							
								                if (handler != null)
							 | 
						||
| 
								 | 
							
								                {
							 | 
						||
| 
								 | 
							
								                    handler.Invoke(data);
							 | 
						||
| 
								 | 
							
								                }
							 | 
						||
| 
								 | 
							
								            }
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |