VSIX window - key shortcut to execute ICommand

前端 未结 3 709
深忆病人
深忆病人 2021-01-21 02:51

Having a Visual Studio extension (VSIX) project: In Window we got UserControl, which have Button binded to some ICommand. Thi

3条回答
  •  星月不相逢
    2021-01-21 03:22

    KeyBindings seems complicated and needs to be definied on several steps (also depends on requirements). This answer is written as a bonus to answer of user1892538.

    Scenario: We got toolWindow which is already showing, but we want to add some command, which will invoke method in the view/view-model.


    1. Create new Command (Step 3 in this tutorial):

    Right-click to the project -> Add New Item -> Custom command. This will create 2 files and modify file with package:

    • CommandName.png - icon for the menu
    • CommandName.cs - class file including the source code of command
    • ProjectWindowPackage.cs - Package class with Initialize() method, which invokes Initialize() of CommandName.cs

    MyWindowPackage.cs:

    public sealed class MyWindowPackage : Package
    {
        public const string PackageGuidString = "00000000-0000-0000-0000-000000000003";
    
        public MyWindowPackage() { }
    
        protected override void Initialize()
        {
            MyToolWindowCommand.Initialize(this);
            MySaveCommand.Initialize(this);
            base.Initialize();
        }
    }
    

    CommandName.cs:

    // these 2 values will do the binding
    public static readonly Guid ApplicationCommands
                                      = new Guid("00000000-0000-0000-0000-000000000002");
    public const int SaveCommandId = 0x0201;
    
    private readonly Package package;
    
    private CommandName(Package package)
    {
        // we need to have package (from Initialize() method) to set VS
        if (package == null) throw new ArgumentNullException("package");
        this.package = package;
    
        // this provides access for the Menu (so we can add our Command during init)
        OleMenuCommandService commandService = this.ServiceProvider.GetService(typeof(IMenuCommandService)) as OleMenuCommandService;
        if (commandService != null)
        {
            // Creates new command "reference" (global ID)
            var menuCommandID = new CommandID(ApplicationCommands, SaveCommandId);
            // Create new command instance (method to invoke, ID of command)
            var menuItem = new MenuCommand(this.Save, menuCommandID);
            // Adding new command to the menu
            commandService.AddCommand(menuItem);
        }
    
        private void Save()
        {
            // Get instance of our window object (param false -> won't create new window)
            ToolWindowPane lToolWindow = this.package.FindToolWindow(typeof(MyToolWindow), 0, false);
            if ((null == lToolWindow) || (null == lToolWindow.Frame)) return;
    
            // Handle the toolWindow's content as Window (our control)
            ((lToolWindow as MyToolWindow)?.Content as MyWindowControl)?.Save();
        }
    }
    

    2. Set content of MyToolWindow to MyWindowControl (done when VSIX created):

    MyToolWindow.cs:

    [Guid("00000000-0000-0000-0000-000000000001")] //GUID of ToolWindow
    public class MyToolWindow : ToolWindowPane
    {
        public MyToolWindow() : base(null)
        {
            this.Content = new MyWindowControl();
        }
    }
    

    3. Set code in code-behind to invoke ViewModel (or do the job itself):

    MyWindowControl.cs:

    public partial class MyWindowControl : UserControl
    {
        // output omitted for brevity
    
        public void Save()
        {
            // Do the call towards view-model or run the code
    
            (this.DataContext as MyViewModel)?.SaveCmd.Execute(null);
        }
    }
    

    4. Set Command with Shortcut so VS know how to handle them:

    In MZTools' article can be found solution how to add Command without seeing it in menu, but if You will go to Tools->Window->Keyboard, You might find them there (so You can set up shortcut).

    I will show both origin Button (for displaying tool window) and 2nd invisible Button used for the shortcut (Keybind) only.

    MyWindowPackage.vsct (in several parts):

    
    
    
        
    
      
    
    
    

    KeyBindings (shortcut definition):

    
        
    
    

    And the Symbols, which set and bind GUID, Command definition and Command logic together:

    
        
        
    
        
        
            
            
        
    
    
    
    
    

    Bonus:

    KeyBinding does have property editor="guidVSStd97", this will set the scope of binding to "GENERAL" (useable in each window). If You can set this to the GUID of Your ToolWindow, it will be used only when the ToolWindow is selected. How it works, is described behind this link. And to acomplish it full, get to this link.

提交回复
热议问题