Ejemplo n.º 1
0
        /// <summary>
        /// Removes the <see cref="CommandAdapter"/> from this command.
        /// </summary>
        /// <param name="adapter">The <see cref="CommandAdapter"/> to remove from this <see cref="Command"/>.</param>
        public virtual void RemoveCommandAdapter(CommandAdapter adapter)
        {
            adapter.ExecuteCommand -= this.OnExecuteAction;
            adapter.UnbindCommand(this);

            adapters.Remove(adapter);
        }
        private CommandAdapter FindAssignableAdapter(Type type)
        {
            CommandAdapter adapter = null;

            foreach (KeyValuePair <Type, Type> pair in map)
            {
                if (pair.Key.IsAssignableFrom(type))
                {
                    adapter = (CommandAdapter)Activator.CreateInstance(pair.Value);
                    break;
                }
            }
            return(adapter);
        }
        /// <summary>
        /// Creates a new <see cref="CommandAdapter"/> instance for the specified invoker type.
        /// </summary>
        /// <param name="invokerType">The invoker type to create an adapter for.</param>
        /// <returns>A new <see cref="CommandAdapter"/> instance for the registered invoker;
        /// null if there is no adapter registration for the required invoker type.</returns>
        public CommandAdapter CreateAdapter(Type invokerType)
        {
            Guard.ArgumentNotNull(invokerType, "invokerType");

            CommandAdapter adapter = null;

            if (map.ContainsKey(invokerType))
            {
                adapter = (CommandAdapter)Activator.CreateInstance(map[invokerType]);
            }
            else
            {
                adapter = FindAssignableAdapter(invokerType);
            }

            return(adapter);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Adds a new invoker for this <see cref="Command"/>.
        /// </summary>
        /// <remarks>The command will use the <see cref="ICommandAdapterMapService"/> to create the
        /// corresponding <see cref="CommandAdapter"/> for the specified invoker.</remarks>
        public virtual void AddInvoker(object invoker, string eventName)
        {
            if (mapService == null)
            {
                throw new CommandException(String.Format(CultureInfo.CurrentCulture,
                                                         Resources.CannotGetMapService, name));
            }

            CommandAdapter adapter = mapService.CreateAdapter(invoker.GetType());

            if (adapter == null)
            {
                throw new CommandException(String.Format(CultureInfo.CurrentCulture,
                                                         Resources.CannotGetCommandAdapter, invoker.GetType()));
            }
            adapter.AddInvoker(invoker, eventName);
            AddCommandAdapter(adapter);
            ownedAdapters.Add(adapter);
        }
Ejemplo n.º 5
0
 /// <summary>
 /// Registers a <see cref="CommandAdapter"/> with this command.
 /// </summary>
 /// <param name="adapter">The <see cref="CommandAdapter"/> to register with this <see cref="Command"/>.</param>
 public virtual void AddCommandAdapter(CommandAdapter adapter)
 {
     adapter.ExecuteCommand += this.OnExecuteAction;
     adapter.BindCommand(this);
     adapters.Add(adapter);
 }