0

In my package I register commands in the following way:

var mcs = _provider.GetService(typeof (IMenuCommandService)) as OleMenuCommandService;

var commandId= new CommandID(myCommandGroup, myCommandId);
_command = new OleMenuCommand(MyAction, commandId);
mcs.AddCommand(_command);

Next, when I want to check what commands are registered in Visual Studio, I'm able to do it using following code:

var dte = (DTE) _provider.GetService(typeof(SDTE));
var commands = dte.Commands.Cast<Command>();
foreach (var command in commands.OrderBy(c => c.Name))
{
    var message = command.ID + "(" + command.Name + "): ";
    foreach (object b in command.Bindings)
    {
        message += b.ToString();
    }
    Log.Instance.AppendLine(message);
}

I can identify mine, because there is key shortcut binded to it, e.g.

1055(Analyze.NavigateForward): Global::Shift+Alt+4 
8194(Analyze.NewPerformanceSession):
...
256(): Global::Ctrl+R, Ctrl+H        // my command, empty brackets - no name defined

Unfortunately, its name is empty. How to associate name with the command?

UPDATE (of what I have in *.vsct file):

<Buttons>
    <Button guid="myCommandGroup" id="myCommandId" type="Button">
        <CommandFlag>DontCache</CommandFlag>
        ...
        <Strings>
            <CanonicalName>MyCommand</CanonicalName>
            <CommandName>MyCommand</CommandName>
            <ButtonText>MyCommand</ButtonText>
        </Strings>
    </Button>
</Buttons>
...
<KeyBindings>
    <KeyBinding guid="myCommandGroup" id="myCommandId" editor="guidVSStd97" 
                mod1="Control" mod2="Control" key1="R" key2="H" />
</KeyBindings>
jwaliszko
  • 16,942
  • 22
  • 92
  • 158

2 Answers2

1

I had the same problem but I think it is a bug in Visual Studio. 80% of my buttons provided a valid Command.Name to the code, the remainder had an empty Command.Name

I found that adding the <LocCanonicalName> property with the same content as <CanonicalName> fixed the problem. So it appears to be random, sometimes CanonicalName is used and sometimes LocCanonicalName. Adding them both fixed the problem for me.

<Buttons>
    <Button guid="myCommandGroup" id="myCommandId" type="Button">
        <CommandFlag>DontCache</CommandFlag>
        ...
        <Strings>
            <CanonicalName>MyCommand</CanonicalName>
            <LocCanonicalName>MyCommand</LocCanonicalName>
            <ButtonText>My Command</ButtonText>
        </Strings>
    </Button>
</Buttons>

nb: Using CommandName appears to make little difference to this requirement

Visual Micro
  • 1,561
  • 13
  • 26
0

In your vsct file when you describe a button, menu or a combo, you can specify the CanonicalName element under the Strings element e.g.:

<Button guid="guidVSPackageCmdSet" id="cmdidMyCommand" priority="0x1000" type="Button">
  <Parent guid="guidSHLMainMenu" id="MyMenuGroup" />
  ...
  <Strings>
    <CanonicalName>NameOfTheCommand</CanonicalName>
    <ButtonText>My super button</ButtonText>
  </Strings>
</Button>

More info in the VSCT Schema reference

Yurii
  • 4,811
  • 7
  • 32
  • 41
  • Hi, thank you but unfortunately the effect is the same - no name exists. In addition I've looked through commands list in tools->options->keyboard for such a canonical name existence, and finally tried to write the name in VS command window, to check if intellisense knows about it. All attempts were unsuccessful. – jwaliszko Jan 21 '15 at 19:46
  • @JaroslawWaliszko, I also tried accessing my command via VS Command Window and there was no intellisense support, BUT the command itself works (I manually typed `Tools.NameOfTheCommand`). Regarding `tools->options->keyboard` - window it uses `CommandName` element not the `CanonicalName `. – Yurii Jan 21 '15 at 22:14