Example #1
0
        internal static void AddCommandRef(IModel model, EventCommand command, EventCommandRef commandRef)
        {
            ModelHandlers handlers = GetModelHandlers(command, model, true);

            lock (handlers) {
                handlers.CommandRefs.Add(new WeakReference <EventCommandRef>(commandRef));
                handlers.CommandRefs.RemoveStaleReferences();
            }
        }
Example #2
0
        private static void OnCommandCanExecute(object sender, CanExecuteEventArgs args)
        {
            ModelHandlers handlers = GetModelHandlers((EventCommand)sender, args.Model, false);

            if (handlers == null)
            {
                return;
            }
            handlers.RaiseCanExecute(sender, args);
        }
Example #3
0
        public static void Subscribe(IModel model, EventCommand command, Action execute, Func <bool> canExecute = null)
        {
            ModelHandlers handlers = GetModelHandlers(command, model, true);

            lock (handlers) {
                handlers.Execute += (s, a) => execute();
                if (canExecute != null)
                {
                    handlers.CanExecute += (s, a) => { a.CanExecute = canExecute(); }
                }
                ;
            }
        }
Example #4
0
        public static void Subscribe <T> (IModel model, EventCommand command, Action <T> execute, Func <T, bool> canExecute = null)
        {
            ModelHandlers handlers = GetModelHandlers(command, model, true);

            lock (handlers) {
                handlers.Execute += InvokeWithParamTypeCheck <ExecuteEventArgs, T>(command,
                                                                                   a => execute((T)a.Parameter));
                if (canExecute != null)
                {
                    handlers.CanExecute += InvokeWithParamTypeCheck <CanExecuteEventArgs, T>(command,
                                                                                             a => a.CanExecute = canExecute((T)a.Parameter));
                }
            }
        }
Example #5
0
        private void RegisterModelHandlers()
        {
            ModelHandlers.Clear();

            var handlerTypes = ReflectionUtils.GetTypesFromAssembly <O365ModelHandlerBase>(Assembly.GetExecutingAssembly());

            foreach (var handlerType in handlerTypes)
            {
                var handlerInstance = Activator.CreateInstance(handlerType) as O365ModelHandlerBase;

                if (handlerInstance != null)
                {
                    if (!ModelHandlers.ContainsKey(handlerInstance.TargetType))
                    {
                        ModelHandlers.Add(handlerInstance.TargetType, handlerInstance);
                    }
                }
            }
        }
Example #6
0
        internal static IEnumerable <EventCommandRef> GetCommandRefs(IModel model, EventCommand command)
        {
            ModelHandlers handlers = GetModelHandlers(command, model, false);

            if (handlers == null)
            {
                return(Enumerable.Empty <EventCommandRef>());
            }

            lock (handlers) {
                var result = new List <EventCommandRef>();
                foreach (var wr in handlers.CommandRefs)
                {
                    EventCommandRef commandRef;
                    if (wr.TryGetTarget(out commandRef))
                    {
                        result.Add(commandRef);
                    }
                }
                return(result);
            }
        }