Agregar un submenú a un menú

Este tutorial se basa en la demostración en Agregar un menú a la barra de menús de Visual Studio mostrando cómo agregar un submenú al menú TestMenu .

Un submenú es un menú secundario que aparece en otro menú. Un submenú se puede identificar mediante la flecha que sigue su nombre. Al hacer clic en el nombre, el submenú y sus comandos se mostrarán.

En este tutorial se crea un submenú en un menú de la barra de menús de Visual Studio y se coloca un nuevo comando en el submenú. El tutorial también implementa el nuevo comando.

Agregar un submenú a un menú

  1. Siga los pasos descritos en Agregar un menú a la barra de menús de Visual Studio para crear el proyecto y el elemento de menú. En los pasos de este tutorial se supone que el nombre del proyecto VSIX es TopLevelMenu.

  2. Abra TestCommandPackage.vsct. En la <Symbols> sección , agregue un <IDSymbol> elemento para el submenú, uno para el grupo de submenú y otro para el comando , todo en el <GuidSymbol> nodo denominado "guidTopLevelMenuCmdSet". Este es el mismo nodo que contiene el <IDSymbol> elemento para el menú de nivel superior.

    <IDSymbol name="SubMenu" value="0x1100"/>
    <IDSymbol name="SubMenuGroup" value="0x1150"/>
    <IDSymbol name="cmdidTestSubCommand" value="0x0105"/>
    
  3. Agregue el submenú recién creado a la <Menus> sección .

    <Menu guid="guidTestCommandPackageCmdSet" id="SubMenu" priority="0x0100" type="Menu">
        <Parent guid="guidTestCommandPackageCmdSet" id="MyMenuGroup"/>
        <Strings>
            <ButtonText>Sub Menu</ButtonText>
            <CommandName>Sub Menu</CommandName>
        </Strings>
    </Menu>
    

    El par GUID/ID del elemento primario especifica el grupo de menús que se generó en Agregar un menú a la barra de menús de Visual Studio y es un elemento secundario del menú de nivel superior.

  4. Agregue el grupo de menús definido en el paso 2 a la <Groups> sección y consíguelo a un elemento secundario del submenú.

    <Group guid="guidTestCommandPackageCmdSet" id="SubMenuGroup" priority="0x0000">
        <Parent guid="guidTestCommandPackageCmdSet" id="SubMenu"/>
    </Group>
    
  5. Agregue un nuevo <Button> elemento a la <Buttons> sección para definir el comando creado en el paso 2 como un elemento del submenú.

    <Button guid="guidTestCommandPackageCmdSet" id="cmdidTestSubCommand" priority="0x0000" type="Button">
        <Parent guid="guidTestCommandPackageCmdSet" id="SubMenuGroup" />
        <Icon guid="guidImages" id="bmpPic2" />
        <Strings>
           <CommandName>cmdidTestSubCommand</CommandName>
           <ButtonText>Test Sub Command</ButtonText>
        </Strings>
    </Button>
    
  6. Compile la solución y comience la depuración. Debería ver la instancia experimental.

  7. Haga clic en TestMenu para ver un nuevo submenú denominado Sub Menu. Haga clic en Sub Menu para abrir el submenú y ver un nuevo comando, Test Sub Command. Observe que al hacer clic en Test Sub Command no hace nada.

Agregar un comando

  1. Abra TestCommand.cs y agregue el siguiente identificador de comando después del identificador de comando existente.

    public const int cmdidTestSubCmd = 0x0105;
    
  2. Agregue el submandago. Busque el constructor de comandos. Agregue las líneas siguientes justo después de la llamada al AddCommand método .

    CommandID subCommandID = new CommandID(CommandSet, cmdidTestSubCmd);
    MenuCommand subItem = new MenuCommand(new EventHandler(SubItemCallback), subCommandID);
    commandService.AddCommand(subItem);
    

    El SubItemCallback controlador de comandos se definirá más adelante. El constructor debería tener ahora este aspecto:

    private TestCommand(Package package)
    {
        if (package == null)
        {
            throw new ArgumentNullException("package");
        }
    
        this.package = package;
    
        OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
        if (commandService != null)
        {
            var menuCommandID = new CommandID(CommandSet, CommandId);
            var menuItem = new MenuCommand(this.MenuItemCallback, menuCommandID);
            commandService.AddCommand(menuItem);
    
            CommandID subCommandID = new CommandID(CommandSet, cmdidTestSubCmd);
            MenuCommand subItem = new MenuCommand(new EventHandler(SubItemCallback), subCommandID);
            commandService.AddCommand(subItem);
        }
    }
    
  3. Agregue SubItemCallback(). Este es el método al que se llama cuando se hace clic en el nuevo comando del submenú.

    private void SubItemCallback(object sender, EventArgs e)
    {
        ThreadHelper.ThrowIfNotOnUIThread();
        IVsUIShell uiShell = this.package.GetService<SVsUIShell, IVsUIShell>();
        Guid clsid = Guid.Empty;
        int result;
        uiShell.ShowMessageBox(
            0,
            ref clsid,
            "TestCommand",
            string.Format(CultureInfo.CurrentCulture,
            "Inside TestCommand.SubItemCallback()",
            this.ToString()),
            string.Empty,
            0,
            OLEMSGBUTTON.OLEMSGBUTTON_OK,
            OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST,
            OLEMSGICON.OLEMSGICON_INFO,
            0,
            out result);
    }
    
  4. Compile la solución y comience la depuración. Debería aparecer la instancia experimental.

  5. En el menú TestMenu , haga clic en Sub Menú y, a continuación, haga clic en Probar subcomando. Debería aparecer un cuadro de mensaje y mostrar el texto "Test Command Inside TestCommand.SubItemCallback()".