如何:查看现有键绑定

更新:2007 年 11 月

Bindings 属性可以使您查看或更改与指定的命令相关联的键绑定。读取该属性时将以对象数组的形式检索命令的当前绑定。每个对象包含一个描述绑定的字符串。

将值设置为 Bindings 属性,把一个或多个新的键绑定分配给指定的命令。有关更多信息,请参见 如何:将命令绑定到单个快捷键如何:将命令绑定到多个快捷键组合

ms228756.alert_note(zh-cn,VS.90).gif说明:

显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您的当前设置或版本。这些过程是使用现用的常规开发设置开发的。若要更改设置,请在“工具”菜单上选择“导入和导出设置”。有关更多信息,请参见 Visual Studio 设置

查看现有键绑定

  1. 使用“Visual Studio 外接程序向导”创建一个新的外接程序。为项目命名,然后单击“确定”启动向导。

    有关使用“Visual Studio 外接程序向导”的更多信息,请参见 如何:创建外接程序

  2. 在“选择编程语言”页上选择“使用 Visual C# 创建外接程序”,以运行下面的 Visual C# 示例,或选择“使用 Visual Basic 创建外接程序”运行 Visual Basic 示例。

  3. 将下面的示例函数粘贴到“Visual Studio 外接程序向导”生成的代码的 Connect 类中。

  4. 按照 如何:编译和运行自动化对象模型代码示例 中的描述从 OnConnection 方法调用函数。

  5. 若要运行外接程序,请单击“工具”菜单上的“外接程序管理器”,选择您创建的外接程序,再单击“确定”。

    显示绑定到 File.NewFile 命令的所有快捷键列表。

示例

下面的示例通过显示绑定到 File.NewFile 命令的所有快捷键来演示 Bindings 的用法。

Public Sub OnConnection(ByVal application As Object, ByVal _
  connectMode As ext_ConnectMode, ByVal addInInst As Object, ByRef _
  custom As Array) Implements IDTExtensibility2.OnConnection
    _applicationObject = CType(application, DTE2)
    _addInInstance = CType(addInInst, AddIn)
    ' Pass the applicationObject member variable to the code example.
    ListKeyBindings(_applicationObject)
End Sub

Sub ListKeyBindings(ByVal dte As DTE2)
    ' Bindings() is an array of key binding string names.
    Dim bindings() As Object
    Dim binding As Object
    Dim msg As String = Nothing
    ' Populate the collection with all of the bindings
    ' for the command File.NewFile.
    bindings = dte.Commands.Item("File.NewFile").Bindings
    For Each binding In bindings
        msg += CStr(binding) & vbCr
    Next
    MsgBox(msg)
 End Sub
// Add-in code.
public void OnConnection(object application,
 Extensibility.ext_ConnectMode connectMode, object addInInst, ref
 System.Array custom)
{
    _applicationObject = (DTE2)application;
    _addInInstance = (AddIn)addInInst;

    //Pass the applicationObject member variable to the code example.
    ListKeyBindings((DTE2)_applicationObject);
}
public void ListKeyBindings(DTE2 dte)
{
    object[] bindings;
    string msg = string.Empty;
    // Populate the collection with all of the bindings associated
    // with the command File.NewFile.
    // Bindings() is an array of key binding string names.
    bindings = (object[])dte.Commands.Item("File.NewFile", 0).Bindings;
    foreach (object b in bindings)
    {
        msg += ((string)b) + "\n";
    }
    System.Windows.Forms.MessageBox.Show(msg);
}

请参见

任务

如何:保留现有命令键绑定

概念

Bindings 属性的参数格式

其他资源

将外接程序命令绑定到键