buttonvisual-studio-extensionsvsct

How do I add a button to a menu in another (custom) extension?


I've got a few VSIX projects that depend on a parent VSIX. In that parent I have a menu and I want to be able to add buttons to that parent menu.

So this is the Parent VSCommandTable.vsct:

<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <Extern href="stdidcmd.h"/>
  <Extern href="vsshlids.h"/>
  <Include href="KnownImageIds.vsct"/>
  <Include href="VSGlobals.vsct"/>

  <Commands package="ParentPackage">
    <Groups>     
      <Group guid="ParentPackage" id="ParentGroup" priority="0x0600">
        <Parent guid="ParentPackage" id="ParentMenu"/>
      </Group>
    </Groups>

    <Menus>     
      <Menu guid="ParentPackage" id="ParentMenu" priority="0x100" type="Menu">
        <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" />
        <Strings>
          <ButtonText>Parent</ButtonText>
        </Strings>
      </Menu>
    </Menus>
    
    <Buttons>
      <Button guid="ParentPackage" id="OptionsCommand" priority="0x0100" type="Button">
        <Parent guid="ParentPackage" id="ParentGroup" />
        <Icon guid="ImageCatalogGuid" id="Settings" />
        <CommandFlag>IconIsMoniker</CommandFlag>
        <Strings>
          <ButtonText>Options</ButtonText>
          <LocCanonicalName>.ParentPackage.OptionsCommand</LocCanonicalName>
        </Strings>
      </Button>      
    </Buttons>
  </Commands>

  <Symbols>
    <GuidSymbol name="ParentPackage" value="{fff82d40-a222-33cd-9145-792a25276511}">
      <IDSymbol name="ParentGroup" value="0x0011" />
      <IDSymbol name="ParentMenu" value="0x0012" />
      <IDSymbol name="OptionsCommand" value="0x0033" />
    </GuidSymbol>
  </Symbols>
</CommandTable>

And this is the Child VSCommandTable.vsct:

<?xml version="1.0" encoding="utf-8"?>
<CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">

    <Extern href="stdidcmd.h"/>
    <Extern href="vsshlids.h"/>
    <Include href="KnownImageIds.vsct"/>
    <Include href="VSGlobals.vsct"/>

    <Commands package="ChildPackage">
        <Groups>
            <Group guid="ChildPackage" id="DummyMenuGroup" priority="0x0600">
                <Parent guid="VSMainMenu" id="Tools"/>
            </Group>
        </Groups>

        <Buttons>
            <Button guid="ChildPackage" id="DummyCommand" priority="0x0100" type="Button">
                <Parent guid="ParentPackage" id="ParentGroup" />
                <Icon guid="ImageCatalogGuid" id="StatusInformation" />
                <CommandFlag>IconIsMoniker</CommandFlag>
                <Strings>
                    <ButtonText>Dummy Command</ButtonText>
                    <LocCanonicalName>.ChildPackage.DummyCommand</LocCanonicalName>
                </Strings>
            </Button>
        </Buttons>
    </Commands>

    <Symbols>
        <GuidSymbol name="ChildPackage" value="{FFF82D40-A852-43CD-9145-792A25276574}">
            <IDSymbol name="DummyMenuGroup" value="0x0001" />
            <IDSymbol name="DummyCommand" value="0x0100" />
        </GuidSymbol>
    </Symbols>
</CommandTable>

And obviously, this line; <Parent guid="ParentPackage" id="ParentGroup" /> does not compile because the ParentPackage guid and id are unknown. So I added an Include to the other vsct file and that seemed to solve that problem..

<Include href="..\ParentProject\VSCommandTable.vsct" />

However, that introduced this monstrosity; Duplicate menu items

I get that I might get a duplicate Options, because that I import the file, but I'm baffled it produces this...? Does anybody have an answer what I do wrong?


Solution

  • Jason Malinowski's comment is the correct answer. Thanks Jason!

    The child vsct is looking like this now and that works like a charm. I'm going to put the symbols in separate files so I don't have to maintain the parents symbols in multiple places, but that should be it!

    <?xml version="1.0" encoding="utf-8"?>
    <CommandTable xmlns="http://schemas.microsoft.com/VisualStudio/2005-10-18/CommandTable" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    
        <Extern href="stdidcmd.h"/>
        <Extern href="vsshlids.h"/>
        <Include href="KnownImageIds.vsct"/>
        <Include href="VSGlobals.vsct"/>
    
        <Commands package="ChildPackage">
            <Groups>
                <Group guid="ChildPackage" id="DummyMenuGroup" priority="0x0600">
                    <Parent guid="VSMainMenu" id="Tools"/>
                </Group>
            </Groups>
    
            <Buttons>
                <Button guid="ChildPackage" id="DummyCommand" priority="0x0100" type="Button">
                    <Parent guid="ParentPackage" id="ParentGroup" />
                    <Icon guid="ImageCatalogGuid" id="StatusInformation" />
                    <CommandFlag>IconIsMoniker</CommandFlag>
                    <Strings>
                        <ButtonText>Dummy Command</ButtonText>
                        <LocCanonicalName>.ChildPackage.DummyCommand</LocCanonicalName>
                    </Strings>
                </Button>
            </Buttons>
        </Commands>
    
        <Symbols>
            <GuidSymbol name="ParentPackage" value="{fff82d40-a222-33cd-9145-792a25276511}">
              <IDSymbol name="ParentGroup" value="0x0011" />
              <IDSymbol name="ParentMenu" value="0x0012" />
              <IDSymbol name="OptionsCommand" value="0x0033" />
            </GuidSymbol>
            <GuidSymbol name="ChildPackage" value="{FFF82D40-A852-43CD-9145-792A25276574}">
                <IDSymbol name="DummyMenuGroup" value="0x0001" />
                <IDSymbol name="DummyCommand" value="0x0100" />
            </GuidSymbol>
        </Symbols>
    </CommandTable>