Show a Visual Studio Extension Toolbar by default

2020-03-28 04:30发布

问题:

In a Visual Studio extension, I have defined a Toolbar in my .vsct file as:

      <Menu guid="guidVsCmdSet" id="MyToolbar" type="Toolbar" >
        <CommandFlag>TextChanges</CommandFlag>
        <CommandFlag>DefaultDocked</CommandFlag>      
        <Strings>
          <ButtonText>My Tools/ButtonText>
        </Strings>
      </Menu>

When I launch my extension in Visual Studio Experimental hive, I can open my Toolbar manually via right clicking in the CommandBar area.

Is there any way to have the toolbar show up by default when I first run the extension?

I have tried a couple other CommandFlags, but they did not seem to perform this behavior:

    <CommandFlag>AlwaysCreate</CommandFlag>
    <CommandFlag>DontCache</CommandFlag>   

回答1:

Read about how to do that in this workaround: LVN! Sidebar #7 - Showing a toolbar at Visual Studio startup



回答2:

What you want to look at is Visibility Constraints. The below example uses GUID_TextEditorFactory which makes it visible when a text editor is active (and hide when, for example a designer is active).

I'm not 100% sure what the correct context is to make it always visible, but my guess is UICONTEXT_NoSolution (or maybe GUID_VSStandardCommandSet97).

<CommandTable>
    <Commands.../>
    <VisibilityConstraints>
        <VisibilityItem guid="guidVsCmdSet" id="MyToolbar" context="GUID_TextEditorFactory" />
    </VisibilityConstraints>
</CommandTable>