/// <summary> /// Adds a menu command to the document. The menu command must already exist /// on a menu; this merely adds a handler for it. /// </summary> public virtual void AddCommand(MenuCommand command) { ArgumentNullException.ThrowIfNull(command); // If the command already exists, it is an error to add // a duplicate. // if (((IMenuCommandService)this).FindCommand(command.CommandID) is not null) { throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, SR.MenuCommandService_DuplicateCommand, command.CommandID.ToString())); } ArrayList commandsList; lock (_commandGroupsLock) { if (!_commandGroups.TryGetValue(command.CommandID.Guid, out commandsList)) { commandsList = new ArrayList(); commandsList.Add(command); _commandGroups.Add(command.CommandID.Guid, commandsList); } else { commandsList.Add(command); } } command.CommandChanged += _commandChangedHandler; Debug.WriteLineIf(MENUSERVICE.TraceVerbose, "Command added: " + command.ToString()); // raise event OnCommandsChanged(new MenuCommandsChangedEventArgs(MenuCommandsChangedType.CommandAdded, command)); }
/// <summary> /// Removes the given menu command from the document. /// </summary> public virtual void RemoveCommand(MenuCommand command) { if (command is null) { throw new ArgumentNullException(nameof(command)); } ArrayList commands; lock (_commandGroupsLock) { if (_commandGroups.TryGetValue(command.CommandID.Guid, out commands)) { int index = commands.IndexOf(command); if (-1 != index) { commands.RemoveAt(index); // If there are no more commands in this command group, remove the group if (commands.Count == 0) { _commandGroups.Remove(command.CommandID.Guid); } command.CommandChanged -= _commandChangedHandler; Debug.WriteLineIf(MENUSERVICE.TraceVerbose, "Command removed: " + command.ToString()); // raise event OnCommandsChanged(new MenuCommandsChangedEventArgs(MenuCommandsChangedType.CommandRemoved, command)); } return; } } Debug.WriteLineIf(MENUSERVICE.TraceVerbose, "Unable to remove command: " + command.ToString()); }