コード例 #1
0
        /// <summary>
        /// Add item to the menu.
        /// </summary>
        /// <param name="vsCommandInfo">The command info.</param>
        protected virtual void AddMenuItem(VSCommandInfo vsCommandInfo)
        {
            TraceService.WriteLine("CommandManager::AddMenuItem Name=" + vsCommandInfo.Name);

            //// This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //// just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                Commands2 commands = (Commands2)this.VSInstance.ApplicationObject.Commands;
                object[]  context  = new object[] { };

                TraceService.WriteLine("CommandManager::AddMenuItem Adding command name=" + vsCommandInfo.Name);

                IEnumerable <Command> currentCommands = commands.Cast <Command>();

                foreach (Command command in currentCommands
                         .Where(command => command.Name == vsCommandInfo.Name))
                {
                    TraceService.WriteLine("CommandManager::AddMenuItem Deleting Command already in memory name=" + vsCommandInfo.Name);
                    command.Delete();
                    break;
                }

                vsCommandInfo.Command = commands.AddNamedCommand2(
                    vsCommandInfo.AddIn,
                    vsCommandInfo.Name,
                    vsCommandInfo.ButtonText,
                    vsCommandInfo.Tooltip,
                    vsCommandInfo.UseOfficeResources,
                    vsCommandInfo.BitmapResourceId,
                    ref context);

                vsCommandInfo.Command.AddControl(vsCommandInfo.ParentCommand, vsCommandInfo.Position);

                this.commandInfos.Add(vsCommandInfo);
            }
            catch (ArgumentException exception)
            {
                //// If we are here, then the exception is probably because a command with that name
                //// already exists. If so there is no need to recreate the command and we can
                //// safely ignore the exception.

                TraceService.WriteError("CommandManager::AddMenuItem ArgumentException");
                TraceService.WriteLine("commandName=" + vsCommandInfo.Name);
                TraceService.WriteError("message=" + exception.Message);
                TraceService.WriteError("parameterName=" + exception.ParamName);
                TraceService.WriteError("stackTrace=" + exception.StackTrace);
            }
        }
コード例 #2
0
        /// <summary>
        /// Add item to the menu.
        /// </summary>
        /// <param name="commandInfo">The command info.</param>
        protected virtual void AddMenuItem(VSCommandInfo commandInfo)
        {
            TraceService.WriteLine("CommandManager::AddMenuItem Name=" + commandInfo.Name);

            //// This try/catch block can be duplicated if you wish to add multiple commands to be handled by your Add-in,
            //// just make sure you also update the QueryStatus/Exec method to include the new command names.
            try
            {
                Commands2 commands = (Commands2)this.VsInstance.ApplicationObject.Commands;
                object[]  context  = { };

                TraceService.WriteLine("AddMenuItem name=" + commandInfo.Name);

                IEnumerable <Command> currentCommands = commands.Cast <Command>();

                foreach (Command command in currentCommands
                         .Where(command => command.Name.Contains(commandInfo.Name)))
                {
                    TraceService.WriteLine("Deleting Command already in memory name=" + commandInfo.Name);
                    command.Delete();
                    break;
                }

                TraceService.WriteLine("AddingCommand");

                commandInfo.Command = commands.AddNamedCommand2(
                    commandInfo.AddIn,
                    commandInfo.Name,
                    commandInfo.ButtonText,
                    commandInfo.Tooltip,
                    commandInfo.UseOfficeResources,
                    commandInfo.BitmapResourceId,
                    ref context);

                TraceService.WriteLine("AddControl");

                commandInfo.Command.AddControl(commandInfo.ParentCommand, commandInfo.Position);

                TraceService.WriteLine("Added");

                this.commandInfos.Add(commandInfo);
            }
            catch (Exception exception)
            {
                TraceService.WriteLine("commandName=" + commandInfo.Name);
                TraceService.WriteLine("message=" + exception.Message);
                TraceService.WriteLine("stackTrace=" + exception.StackTrace);
            }
        }
コード例 #3
0
        /// <summary>
        /// Removes the menu item.
        /// </summary>
        /// <param name="menuItemName">Name of the menu item.</param>
        protected void RemoveMenuItem(string menuItemName)
        {
            TraceService.WriteLine("CommandManager::RemoveMenuItem name=" + menuItemName);

            try
            {
                Commands2 commands = (Commands2)this.VsInstance.ApplicationObject.Commands;

                IEnumerable <Command> currentCommands = commands.Cast <Command>();

                foreach (Command command in currentCommands.Where(command => command.Name == menuItemName))
                {
                    TraceService.WriteLine("CommandManager::RemoveMenuItem Deleting Command name=" + menuItemName);
                    command.Delete();
                    break;
                }
            }
            catch (Exception exception)
            {
                TraceService.WriteLine("menuItemName=" + menuItemName);
                TraceService.WriteLine("stackTrace=" + exception.StackTrace);
            }
        }