VS 2013 SDK:处理代码 window 的上下文菜单打开事件?

VS 2013 SDK: Handle code window's contextmenu opening event?

C#VB.Net 中,使用 Visual Studio Package,我如何处理当前 代码 window 编辑器 的上下文菜单正在打开 and/or 完全打开时引发的事件?

我的意图是通过在上下文菜单打开或完全打开时评估条件来任意 enable/disable 我的 CommandBarPopup 菜单内的 CommandBarButton 按钮。

但是我找不到任何相关信息,无论是事件名称还是SDK中的class都没有涉及这个问题。


更新

这是遵循@Carlos Quintero 指示的代码示例,但是,在我按下按钮一次并完成回调之前,BeforeQueryStatus 事件永远不会引发,为什么?如何解决?

如我所说,我需要能够在代码编辑器的上下文菜单打开时控制按钮状态 (enabled/disabled)。

Protected Overrides Sub Initialize()

    Debug.WriteLine(String.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", Me.GetType().Name))
    MyBase.Initialize()

    ' Add our command handlers for menu (commands must exist in the .vsct file)
    Dim mcs As OleMenuCommandService = TryCast(GetService(GetType(IMenuCommandService)), OleMenuCommandService)

    If Not mcs Is Nothing Then

        ' Create the command for the menu item.
        Dim menuCommandID As New CommandID(GuidList.GuidSnippetToolCmdSet, CInt(PkgCmdIDList.cmdidMyCommand))
        Dim menuItem As New OleMenuCommand(New EventHandler(AddressOf MenuItemCallback), menuCommandID)
        AddHandler menuItem.BeforeQueryStatus, AddressOf OnBeforeQueryStatus

        mcs.AddCommand(menuItem)
    End If

End Sub

Private Sub MenuItemCallback(ByVal sender As Object, ByVal e As EventArgs)

    ' Show a Message Box to prove we were here
    Dim uiShell As IVsUIShell = TryCast(GetService(GetType(SVsUIShell)), IVsUIShell)
    Dim clsid As Guid = Guid.Empty
    Dim result As Integer
    Microsoft.VisualStudio.ErrorHandler.ThrowOnFailure(uiShell.ShowMessageBox(0, clsid, "Snippet Tool", String.Format(CultureInfo.CurrentCulture, "Inside {0}.MenuItemCallback()", Me.GetType().Name), String.Empty, 0, OLEMSGBUTTON.OLEMSGBUTTON_OK, OLEMSGDEFBUTTON.OLEMSGDEFBUTTON_FIRST, OLEMSGICON.OLEMSGICON_INFO, 0, result))

End Sub

Private Sub OnBeforeQueryStatus(ByVal sender As Object, ByVal e As EventArgs)

    Dim myCommand As OleMenuCommand = TryCast(sender, OleMenuCommand)

    ' Alternate the command enable, just for testing.
    myCommand.Enabled = Not myCommand.Enabled

End Sub

上下文菜单没有此类事件。当发生某些事情时(例如显示上下文菜单、更改选择、关闭解决方案等),VS 会查询所有命令的状态。

做你想做的事情的方法是使用OleMenuCommand, BeforeQueryStatus event. In that event (that you don't know what caused it) you set the properties (enabled, visible, etc.) of the command depending on your conditions. See: How to: Create and Handle Commands in VSPackages (C#)