且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何制作运行 VSIX 方法的快捷方式?

更新时间:2023-01-31 11:15:31

您可以向 .vsct 文件添加快捷方式.当您生成一个新扩展并说它将有一个菜单命令时,该文件会在向导中自动创建.手动添加:

You can add shortcuts to the .vsct file. This file is created automatically in the wizard when you generate a new extension and say it will have a menu command. To add it manually:

  • 创建 MyCommands.vsct
  • 将文件属性设置为 VSCTCompile
  • 卸载您的项目,右键单击并编辑项目:
<VSCTCompile Include="MyCommands.vsct">
    <ResourceName>Menus.ctmenu</ResourceName>
    <SubType>Designer</SubType>
</VSCTCompile>

  • 声明您的项目将具有菜单和快捷方式:
[ProvideMenuResource("Menus.ctmenu",1)]
public sealed class MyPackage : Package

  • 添加一个键绑定部分:
<KeyBindings>
   <KeyBinding guid="yourCmdSet" id="cmdAwesome"
    editor="guidVSStd97"
    key1="VK_F7" mod1="Control Alt"
    key2="VK_F1">
   </KeyBinding>
</KeyBindings>

  • 在您的 Package.Initialize 中:
// Add our command handlers for menu/shortcut (commands must exist in the .vsct file)
OleMenuCommandService mcs = GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
if (null != mcs)
{
    //// Create the command for the menu item.
    var menuCommandID = new CommandID(GuidList.yourCmdSet,(int)PkgCmdIDList.cmdAwesome);
    var menuItem = new MenuCommand((sender, evt) =>
    {
        // Do stuff
    }
}

更多资源: