如何:响应特定项目中的事件 (Visual Basic)

自动化模型包括可用于响应 Visual Studio 集成开发环境 (IDE) 中的环境事件的对象。 在特定于 Visual C# 和 Visual Basic 项目的 VSLangProjVSLangProj80 中定义了环境事件。 例如,在向 Imports 集合添加导入或从中移除导入时会引发 ImportsEvents

此示例使用 Visual Basic 将某类项目特定的 ReferencesEvents 事件处理程序添加到外接程序项目中。 在更改引用以及在 Visual C# 或 Visual Basic 项目中添加或移除引用时,将引发 ReferencesEvents

备注

以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。您安装的 Visual Studio 版本以及使用的设置决定了这些元素。有关详细信息,请参阅 在 Visual Studio 中自定义开发设置

使用 Visual Basic 处理与引用相关的事件

  1. 在 Visual Basic 中创建 Visual Studio 外接程序项目。

  2. 将 Imports VSLangProj 添加到 Connect.vb 文件的顶部。

  3. 在**“项目”菜单上,单击“添加引用”,再单击“.NET”选项卡,然后选择第一个“VSLangProj”,并单击“确定”**。

  4. 在 Connect 类中,初始化一个变量以处理 ReferencesEvents 对象,初始化另一个变量以处理 OutputWindowPane

    Public Class Connect
        Implements IDTExtensibility2
        Dim _applicationObject As DTE2
        Dim _addInInstance As AddIn
        ' Handle Reference events.
        Public WithEvents refEvents As VSLangProj.ReferencesEvents
        Private outputWinPane As OutputWindowPane
    

    在此示例中,变量被命名为 refEvents。

    自动化模型中的其他对象与特定于项目的其他类型的事件相关。 例如,在向 Imports 集合添加导入或从中移除导入时会引发 ImportsEventsBuildManagerEvents 应用于根据自定义工具的输出生成的临时程序集的相关事件。 有关 BuildManager 对象的更多信息,请参见 BuildManager 对象介绍。 有关特定于项目类型的事件的完整列表,请参见 事件对象(特定于项目的类型)。 有关通用自动化事件的列表,请参见 自动化事件对象

  5. OnConnection 方法中,初始化一个变量以截获事件。 在此示例中,变量被命名为 events。

        Dim events As EnvDTE80.Events2
        events = CType(_applicationObject.Events, Events2)
    
  6. OnConnection 方法中,初始化 OutputWindow 变量。

        Dim outputWindow As OutputWindow
        outputWindow = CType(_applicationObject.Windows.Item _
        (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
        outputWinPane = outputWindow.OutputWindowPanes.Add_
        ("Reference Event Information ")
    
  7. 此外,在 OnConnection 方法中检索来自自动化模型的事件对象。

    refEvents = CType(events.GetObject("CSharpReferencesEvents"),_
     ReferencesEvents)
    

    因为该对象变量声明使用 WithEvents (Visual Basic) 处理程序,所以 Visual Studio 会自动连接此方法处理程序。

    在本示例中,ReferencesEvents 特定于 Visual C# 项目。 若要响应特定于 Visual Basic 事件的事件,请将字符串 CSharpReferencesEvents 替换为 VBReferencesEvents。 有关如何确定应为特定于不同类型的项目的事件使用哪些字符串的更多信息,请参见 事件对象(特定于项目的类型)

  8. 为每个与事件对象相关的事件添加过程。 例如,若要处理添加引用时发生的事件,请使用:

    Sub ReferenceAdded(ByVal addedRef As VSLangProj.Reference)_
        Handles refEvents.ReferenceAdded
        outputWinPane.OutputString_
        ("ReferencesEvents.ReferenceAdded" & ControlChars.Lf)
        outputWinPane.OutputString("The reference to " _
        & addedRef.Name & " was added." & ControlChars.Lf)
    End Sub
    

    如果使用 ReferencesEvents,则必须为 ReferenceAddedReferenceRemovedReferenceChanged 定义事件,正如下面示例中的完整列表所示。

  9. 最后,为了防止在关闭外接程序后 Visual Studio 继续监视与窗口相关的事件而减慢系统的运行速度,应禁用事件处理。 在 Visual Basic 中,这可以通过将事件处理程序设置为 Nothing 来实现。

    Public Sub OnDisconnection(ByVal RemoveMode As_
     Extensibility.ext_DisconnectMode, ByRef custom As System.Array)_
     Implements Extensibility.IDTExtensibility2.OnDisconnection
        refEvents = Nothing
    End Sub
    

    这样,无论是关闭外接程序还是在外接程序仍然运行的情况下关闭 IDE,都会关闭事件处理。 关闭 IDE 后,所有正在运行的外接程序将首先自动关闭。

示例

下面的示例是一个简单的 Visual Studio 外接程序,它演示如何在 Visual Studio 中截获和处理 Visual C# 引用事件。 每当发生引用事件时,就会向**“输出”**窗口发送一则通知消息。

Imports System
Imports Microsoft.VisualStudio.CommandBars
Imports Extensibility
Imports EnvDTE
Imports EnvDTE80
Imports VSLangProj

Public Class Connect
    Implements IDTExtensibility2
    Dim _applicationObject As DTE2
    Dim _addInInstance As AddIn
    ' Handle Reference events.
    Public WithEvents refEvents As VSLangProj.ReferencesEvents
    Private outputWinPane As OutputWindowPane
Public Sub OnBeginShutdown(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnBeginShutdown
End Sub

Public Sub OnAddInsUpdate(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnAddInsUpdate
End Sub

Public Sub OnStartupComplete(ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnStartupComplete
End Sub

Public Sub OnDisconnection(ByVal RemoveMode As Extensibility.ext_DisconnectMode, ByRef custom As System.Array) _
 Implements Extensibility.IDTExtensibility2.OnDisconnection
    ' Turns off reference event handling when the add-in shuts down.
    refEvents = Nothing
End Sub

Public Sub OnConnection(ByVal application As Object, _
 ByVal connectMode As Extensibility.ext_ConnectMode, ByVal addInInst As Object, ByRef _
 custom As System.Array) Implements _
 Extensibility.IDTExtensibility2.OnConnection

    _applicationObject = CType(application, EnvDTE80.DTE2)
    _addInInstance = CType(addInInst, EnvDTE.AddIn)

    Dim events As EnvDTE80.Events2
    events = CType(_applicationObject.Events, Events2)
    ' Send event messages to the Output window.
    Dim outputWindow As OutputWindow
    outputWindow = CType(_applicationObject.Windows.Item _
    (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow)
    outputWinPane = outputWindow.OutputWindowPanes.Add _
    ("Reference Event Information ")

    ' Retrieve the event objects from the automation model.
    ' Visual Basic automatically connects the method handler since 
    ' the object variable declaration uses the 'WithEvents' handler.
    refEvents = CType(events.GetObject("CSharpReferencesEvents"), _
    ReferencesEvents)
End Sub
    ' Handle all of the various reference-related events.
    Sub ReferenceRemoved(ByVal removedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceRemoved
    outputWinPane.OutputString("ReferencesEvents.ReferenceRemoved" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & removedRef.Name _
    & " was removed." & ControlChars.Lf)
End Sub

Sub ReferenceChanged(ByVal changedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceChanged
    outputWinPane.OutputString("ReferencesEvents.ReferenceChanged" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & changedRef.Name _
    & " was changed." & ControlChars.Lf)
End Sub
Sub ReferenceAdded(ByVal addedRef As VSLangProj.Reference) _
    Handles refEvents.ReferenceAdded
    outputWinPane.OutputString("ReferencesEvents.ReferenceAdded" _
    & ControlChars.Lf)
    outputWinPane.OutputString("The reference to " & addedRef.Name _
    & " was added." & ControlChars.Lf)
End Sub

End Class

编译代码

若要编译此代码,请在 Visual Basic 中新建 Visual Studio 外接程序项目,然后用该示例中的代码替换 Connect 类的代码。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序

请参见

任务

如何:响应特定项目中的事件 (Visual C#)

参考

Imports 语句(.NET 命名空间和类型)

其他资源

响应自动化事件

响应事件(Visual Basic 和 Visual C# 项目)