自动化模型包括可用于响应 Visual Studio 集成开发环境 (IDE) 中的环境事件的对象。 在特定于 Visual C# 和 Visual Basic 项目的 VSLangProj 和 VSLangProj80 中定义了环境事件。 例如,在向 Imports 集合添加导入或从中移除导入时会引发 ImportsEvents。
此示例使用 Visual Basic 将某类项目特定的 ReferencesEvents 事件处理程序添加到外接程序项目中。 在更改引用以及在 Visual C# 或 Visual Basic 项目中添加或移除引用时,将引发 ReferencesEvents。
提示
显示的对话框和菜单命令可能会与“帮助”中的描述不同,具体取决于您现用的设置或版本。 这些过程是在“常规开发设置”处于活动状态时开发的。 若要更改设置,请在“工具”菜单上选择“导入和导出设置”。 有关更多信息,请参见 使用设置。
使用 Visual Basic 处理与引用相关的事件
- 在 Visual Basic 中创建 Visual Studio 外接程序项目。 
- 将 Imports VSLangProj 添加到 Connect.vb 文件的顶部。 
- 在**“项目”菜单上,单击“添加引用”,再单击“.NET”选项卡,然后选择第一个“VSLangProj”,并单击“确定”**。 
- 在 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 集合添加导入或从中移除导入时会引发 ImportsEvents。 BuildManagerEvents 应用于根据自定义工具的输出生成的临时程序集的相关事件。 有关 BuildManager 对象的更多信息,请参见 BuildManager 对象介绍。 有关特定于项目类型的事件的完整列表,请参见 事件对象(特定于项目的类型)。 有关通用自动化事件的列表,请参见 自动化事件对象。 
- 在 OnConnection 方法中,初始化一个变量以截获事件。 在此示例中,变量被命名为 events。 - Dim events As EnvDTE80.Events2 events = CType(_applicationObject.Events, Events2)
- 在 OnConnection 方法中,初始化 OutputWindow 变量。 - Dim outputWindow As OutputWindow outputWindow = CType(_applicationObject.Windows.Item _ (Constants.vsWindowKindOutput).Object, EnvDTE.OutputWindow) outputWinPane = outputWindow.OutputWindowPanes.Add_ ("Reference Event Information ")
- 此外,在 OnConnection 方法中检索来自自动化模型的事件对象。 - refEvents = CType(events.GetObject("CSharpReferencesEvents"),_ ReferencesEvents)- 因为该对象变量声明使用 WithEvents (Visual Basic) 处理程序,所以 Visual Studio 会自动连接此方法处理程序。 - 在本示例中,ReferencesEvents 特定于 Visual C# 项目。 若要响应特定于 Visual Basic 事件的事件,请将字符串 CSharpReferencesEvents 替换为 VBReferencesEvents。 有关如何确定应为特定于不同类型的项目的事件使用哪些字符串的更多信息,请参见 事件对象(特定于项目的类型)。 
- 为与事件对象相关的每个事件添加过程。 例如,若要处理添加引用时发生的事件,请使用: - 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,则必须为 ReferenceAdded、ReferenceRemoved 和 ReferenceChanged 定义事件,正如下面示例中的完整列表所示。 
- 最后,为了防止在关闭外接程序后 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 类的代码。 有关如何运行外接程序的信息,请参见如何:使用外接程序管理器控制外接程序。