Sunday, September 01, 2013

Missing MenuCommand

I’ve spent the last few days trawling the net, and performing a heap of different experiments - trying to figure out why my custom MenuCommand wasn’t getting registered properly in my Visual Studio Shell application. Turns out it had nothing to do with incorrect GUIDs, or registry entries…

If you want to expose a custom command from your VS Package you need to use the ProvideMenuResourceAttribute. The C# VS Package wizard in VS2012 will correctly apply this attribute to the generated package if you tick the “Menu Command” option on one of the pages. This is what the wizard will put in .cs file that contains the generated package:

[ProvideMenuResource("Menus.ctmenu", 1)]

You may wonder what Menus.ctmenu refers to, but you won’t find it defined in any of the generated source or resource files… unless you look at the contents of the .csproj file generated for the package in a text editor, in which case you’ll find something like this:

<VSCTCompile Include="MyPackage.vsct"> 

The ResourceName for your .vsct must match the first argument of the ProvideMenuResource attribute! If you use the wizard and tick the right option you’re all set, but if you decide to add some commands to an existing package you need to keep this in mind.

In my case the ResourceName element was missing from the .csproj file, and VS2012 doesn’t seem to provide any way to specify it via the UI so I had to add it in by hand.