Exemplo n.º 1
0
        protected override void RegisterCommandInfo(CommandInfo info)
        {
            // Embedded image resources will not be available as WPF app resources
            // If image resource does not exist we need to create it and add it to app resources
            object imageResourceKey = null;
            if ((info.ImageKey == null) && (!string.IsNullOrEmpty(info.ImageName)))
            {
                imageResourceKey = Sce.Atf.Wpf.ResourceUtil.GetKeyFromImageName(info.ImageName);
                info.ImageKey = imageResourceKey;
            }

            base.RegisterCommandInfo(info);

            // Associate the registered MenuInfo with this CommandService.  Only can be registered once.
            info.CommandService = this;

            // If command has not been previously registered then create 
            // a CommandItem for it and add it to the composition
            CommandItem command;
            if (!m_commandsLookup.TryGetValue(info.CommandTag, out command))
            {
                command = new CommandItem(info, CanExecuteCommand, ExecuteCommand);
                m_commandsLookup.Add(command.CommandTag, command);

                if (m_composer != null)
                    command.ComposablePart = m_composer.AddPart(command);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates and registers a command for a command client</summary>
        /// <param name="def">Command definition</param>
        /// <param name="client">Command client</param>
        /// <returns>ICommandItem for command</returns>
        public ICommandItem RegisterCommand(CommandDef def, ICommandClient client)
        {
            Requires.NotNull(client, "client");

            // Problem - what about same command tag registered in several places with different shortcuts
            // submenus etc?
            ICommandItem command;
            if (!m_commandsLookup.TryGetValue(def.CommandTag, out command))
            {
                if (!CommandIsUnique(def.MenuTag, def.CommandTag))
                {
                    throw new InvalidOperationException(
                        string.Format(
                            "Duplicate menu/command combination. CommandTag: {0}, MenuTag: {1}, GroupTag: {2}, MenuText: {3}",
                            def.CommandTag, def.GroupTag, def.MenuTag, def.Text));
                }

                command = new CommandItem(def, CanDoCommand, CommandExecuted);
                m_commands.Add(command);
                m_commands.Sort(new CommandComparer());
                m_commandsLookup.Add(command.CommandTag, command);
                int index = m_commands.IndexOf(command);
                CommandAdded.Raise<ItemInsertedEventArgs<ICommandItem>>(this, new ItemInsertedEventArgs<ICommandItem>(index, command));
            }

            m_commandClients.Add(def.CommandTag, client);

            return command;
        }