Ejemplo n.º 1
0
        /// <summary>
        /// Invoked to unhook an object from the command adapter.
        /// </summary>
        /// <param name="invoker">Object whose event will be unhooked</param>
        /// <param name="eventName">Name of the event to unhook</param>
        public override void RemoveInvoker(object invoker, string eventName)
        {
            Guard.ArgumentNotNull(invoker, "invoker");
            Guard.ArgumentNotNullOrEmptyString(eventName, "eventName");
            ThrowIfWrongType(invoker);

            // get the specific invoker
            TInvoker specificInvoker = (TInvoker)invoker;

            if (this.invoker != specificInvoker)
            {
                throw new InvalidOperationException(Properties.Resources.DifferentInvoker);
            }

            // unhook the event of the cached invoker
            this.UnhookEvent(this.eventSource, eventName);

            // unregister the invoker key & command adapter
            CommandAdapterManager.UnregisterAdapter(this, this.logicalInvoker);

            // release all references regarding the invoker and the events
            this.eventName      = null;
            this.invoker        = null;
            this.eventSource    = null;
            this.logicalInvoker = null;
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Informs the <see cref="CommandAdapter"/> that has been unbinded from the <see cref="Command"/>.
        /// </summary>
        /// <param name="command">The <see cref="Command"/> from with to unbind.</param>
        public override void UnbindCommand(Command command)
        {
            // let the base class unbind the command
            base.UnbindCommand(command);

            // let the master list know that the adapter is no longer associated with a command since it may affect the state of the associated elements
            CommandAdapterManager.NotifyCommandChanged(this, this.command, null);

            // release the reference to the command
            this.command = null;

            // let the derived class know since the element state
            // may need to be updated
            this.OnCommandChanged(null);
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Informs the <see cref="CommandAdapter"/> that has been adapted to the <see cref="Command"/>.
        /// </summary>
        /// <param name="command">The <see cref="Command"/> to bind to.</param>
        public override void BindCommand(Command command)
        {
            // let the base class bind to the command
            base.BindCommand(command);

            // let the master list know that the adapter is associated with a command since it may affect the state of the associated elements
            CommandAdapterManager.NotifyCommandChanged(this, this.command, command);

            // store a reference to the command
            this.command = command;

            // let the derived class know since the element state
            // may need to be updated
            this.OnCommandChanged(command);
        }
Ejemplo n.º 4
0
        /// <summary>
        /// Invoked to associated the command adapter with an object whose event will trigger the execution of the associated command.
        /// </summary>
        /// <param name="invoker">Object whose event will be hooked</param>
        /// <param name="eventName">Name of the event to hook</param>
        public override void AddInvoker(object invoker, string eventName)
        {
            Guard.ArgumentNotNull(invoker, "invoker");
            Guard.ArgumentNotNullOrEmptyString(eventName, "eventName");
            ThrowIfWrongType(invoker);

            if (this.invoker != null)
            {
                throw new InvalidOperationException(Properties.Resources.AdapterAlreadyHasInvoker);
            }

            // get the specific invoker
            TInvoker specificInvoker = (TInvoker)invoker;

            // get the object whose event will be caught
            object eventSource = this.GetEventSource(specificInvoker, eventName);

            if (eventSource == null)
            {
                throw new InvalidOperationException(Properties.Resources.InvalidInvokerEvent);
            }

            // hook the event of the event source. we do this first
            // in case the event doesn't exist, etc.
            this.HookEvent(eventSource, eventName);

            // then cache the information so we can unhook
            this.eventName   = eventName;                       // name of the event hooked
            this.invoker     = specificInvoker;                 // element associated with the adapter
            this.eventSource = eventSource;                     // object whose event we caught

            // create an object that is used to identify the item
            // we do this so if someone creates multiple adapters for the
            // same logical object, we have all the commands associated
            // with that object
            this.logicalInvoker = this.CreateLogicalInvoker(specificInvoker);

            // register the invoker key and adapter
            CommandAdapterManager.RegisterAdapter(this, this.logicalInvoker);
        }