visual-studio-extensionsvsixvsct

Can I dynamically change text of my Visual Studio extension's top level menu (VSIX Community Toolkit)?


I'm adding a top-level menu via my custom Visual Studio 2022 extension and I'd like to change its text depending on some dynamic information my VSIX will check. I tried adapting the instructions from Microsoft's docs but the text doesn't change when I run it (https://learn.microsoft.com/en-us/visualstudio/extensibility/changing-the-text-of-a-menu-command?view=vs-2022)

Here's how the menu is defined in my vsct file:

    <Commands package="DevBuddy">
        <Menus>
            <Menu guid="DevBuddy" id="TopLevelMenu" priority="0x0700" type="Menu">
                <Parent guid="guidSHLMainMenu" id="IDG_VS_MM_TOOLSADDINS" />
                <CommandFlag>TextChanges</CommandFlag>
                <Strings>
                    <ButtonText>DevBuddy</ButtonText>
                </Strings>
            </Menu>
        </Menus>

        <Groups>
            <Group guid="DevBuddy" id="DevBuddyMenuGroup" priority="0x0600">
                <Parent guid="DevBuddy" id="TopLevelMenu"/>
            </Group>
        </Groups>

        <!--This section defines the elements the user can interact with, like a menu command or a button
        or combo box in a toolbar. -->
        <Buttons>
            <Button guid="DevBuddy" id="TestCommand" priority="0x0100" type="Button">
                <Parent guid="DevBuddy" id="DevBuddyMenuGroup" />
                <Icon guid="ImageCatalogGuid" id="StatusInformation" />
                <CommandFlag>IconIsMoniker</CommandFlag>
                <Strings>
                    <ButtonText>Test Command</ButtonText>
                    <LocCanonicalName>.DevBuddy.TestCommand</LocCanonicalName>
                </Strings>
            </Button>
        </Buttons>
    </Commands>

    <Symbols>
        <GuidSymbol name="DevBuddy" value="{GUID}">
            <IDSymbol name="TopLevelMenu" value="0x1021"/>
            <IDSymbol name="DevBuddyMenuGroup" value="0x0001" />
            <IDSymbol name="TestCommand" value="0x0100" />
        </GuidSymbol>
    </Symbols>
</CommandTable>

I created a Command class for the top-level menu:

namespace DevBuddy.Commands
{
    [Command(PackageIds.TopLevelMenu)]
    internal sealed class TopLevelMenu : BaseCommand<TopLevelMenu>
    {
        protected override void BeforeQueryStatus(EventArgs e)
        {
            //base.BeforeQueryStatus(e);
            this.Command.Text = "Dynamic Text!";
        }
    }
}

I expected the menu would show the new text "Dynamic Text!" but it still shows the text defined in the vsct: "DevBuddy".

Is it even possible to change the text of a top-level menu? If so, where did I go wrong?


Solution

  • Looks like I found the answer. I had to add the following attribute to my package class:

    [ProvideAutoLoad(UIContextGuids80.SolutionExists, PackageAutoLoadFlags.BackgroundLoad)]
    

    Apparently the extension is not loaded until a command is executed. Adding this attribute causes the extension to autoload when a solution file is opened.