Beispiel #1
0
        public static RoutedEvent RegisterRoutedEvent(string name, RoutingStrategy routingStrategy, Type handlerType, Type ownerType)
        {
            if (string.IsNullOrEmpty(name))
            {
                throw new ArgumentException("name");
            }
            if (null == handlerType)
            {
                throw new ArgumentNullException("handlerType");
            }
            if (null == ownerType)
            {
                throw new ArgumentNullException("ownerType");
            }
            //
            RoutedEventKey key = new RoutedEventKey(name, ownerType);

            if (routedEvents.ContainsKey(key))
            {
                throw new InvalidOperationException("This routed event is already registered.");
            }
            RoutedEvent     routedEvent     = new RoutedEvent(handlerType, name, ownerType, routingStrategy);
            RoutedEventInfo routedEventInfo = new RoutedEventInfo(routedEvent);

            routedEvents.Add(key, routedEventInfo);
            return(routedEvent);
        }
Beispiel #2
0
        public static void AddHandler(object target, RoutedEvent routedEvent, Delegate handler, bool handledEventsToo)
        {
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }
            if (null == routedEvent)
            {
                throw new ArgumentNullException("routedEvent");
            }
            if (null == handler)
            {
                throw new ArgumentNullException("handler");
            }
            //
            RoutedEventKey key = routedEvent.Key;

            if (!routedEvents.ContainsKey(key))
            {
                throw new ArgumentException("Specified routed event is not registered.", "routedEvent");
            }
            RoutedEventInfo routedEventInfo = routedEvents[key];
            bool            needAddTarget   = true;

            if (routedEventInfo.targetsList != null)
            {
                RoutedEventTargetInfo targetInfo = routedEventInfo.targetsList.FirstOrDefault(info => info.target == target);
                if (null != targetInfo)
                {
                    if (targetInfo.handlersList == null)
                    {
                        targetInfo.handlersList = new List <DelegateInfo>();
                    }
                    targetInfo.handlersList.Add(new DelegateInfo(handler, handledEventsToo));
                    needAddTarget = false;
                }
            }
            if (needAddTarget)
            {
                RoutedEventTargetInfo targetInfo = new RoutedEventTargetInfo(target);
                targetInfo.handlersList = new List <DelegateInfo>();
                targetInfo.handlersList.Add(new DelegateInfo(handler, handledEventsToo));
                if (routedEventInfo.targetsList == null)
                {
                    routedEventInfo.targetsList = new List <RoutedEventTargetInfo>();
                }
                routedEventInfo.targetsList.Add(targetInfo);
            }
        }
Beispiel #3
0
        /// <summary>
        /// Возвращает список таргетов, подписанных на указанное RoutedEvent.
        /// </summary>
        private static List <RoutedEventTargetInfo> getTargetsSubscribedTo(RoutedEvent routedEvent)
        {
            if (null == routedEvent)
            {
                throw new ArgumentNullException("routedEvent");
            }
            RoutedEventKey key = routedEvent.Key;

            if (!routedEvents.ContainsKey(key))
            {
                throw new ArgumentException("Specified routed event is not registered.", "routedEvent");
            }
            RoutedEventInfo routedEventInfo = routedEvents[key];

            return(routedEventInfo.targetsList);
        }
Beispiel #4
0
        public static void RemoveHandler(object target, RoutedEvent routedEvent, Delegate handler)
        {
            if (null == target)
            {
                throw new ArgumentNullException("target");
            }
            if (null == routedEvent)
            {
                throw new ArgumentNullException("routedEvent");
            }
            if (null == handler)
            {
                throw new ArgumentNullException("handler");
            }
            //
            RoutedEventKey key = routedEvent.Key;

            if (!routedEvents.ContainsKey(key))
            {
                throw new ArgumentException("Specified routed event is not registered.", "routedEvent");
            }
            RoutedEventInfo routedEventInfo = routedEvents[key];

            if (routedEventInfo.targetsList == null)
            {
                throw new InvalidOperationException("Targets list is empty.");
            }
            RoutedEventTargetInfo targetInfo = routedEventInfo.targetsList.FirstOrDefault(info => info.target == target);

            if (null == targetInfo)
            {
                throw new ArgumentException("Target not found in targets list of specified routed event.", "target");
            }
            if (null == targetInfo.handlersList)
            {
                throw new InvalidOperationException("Handlers list is empty.");
            }
            int findIndex = targetInfo.handlersList.FindIndex(info => info.@delegate == handler);

            if (-1 == findIndex)
            {
                throw new ArgumentException("Specified handler not found.", "handler");
            }
            targetInfo.handlersList.RemoveAt(findIndex);
        }