![]() |
||||
|
This article describes how to get Project and ProjectItem events (ItemAdded, ItemRemoved, ItemRenamed) from a Visual Studio .NET add-in. More Information The extensibility model of Visual Studio .NET provides the EnvDTE.ProjectEvents and EnvDTE.ProjectItemsEvents classes with the events ItemAdded, ItemRemoved and ItemRenamed, but the class EnvDTE.Events returned by the property EnvDTE.DTE.Events does not provide a direct way to get an instance of that class. The reason is that those events are language specific (C#, VB.NET, etc.), and the package installed for each language must provide those events through automation. The way to get an instance of the EnvDTE.ProjectEvents and EnvDTE.ProjectItemsEvents class is through the EnvDTE.Events.GetObject method, which receives the "name" of the events. Some common names for the Project events are:
Some common names for the ProjectItem events are:
Note: Visual Studio .NET throws exceptions trying to retrieve events for smart device projects (both C# and VB.NET) although the names are correct. This seems to be a bug. To guess the names of other Project or ProjectItem events (or other automation events in general), you can do the following:
The following add-in sample (VB.NET) shows how to show a message box each time that a VB.NET or C# file is added to a project: Public Class Connect
Implements Extensibility.IDTExtensibility2
Private m_objDTE As EnvDTE.DTE
Private WithEvents m_objVBProjectItemsEvents As ProjectItemsEvents
Private WithEvents m_objCSharpProjectItemsEvents As ProjectItemsEvents
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
Try
m_objDTE = CType(application, EnvDTE.DTE)
m_objVBProjectItemsEvents = CType( _
m_objDTE.Events.GetObject("VBProjectItemsEvents"), ProjectItemsEvents)
m_objCSharpProjectItemsEvents = CType( _
m_objDTE.Events.GetObject("CSharpProjectItemsEvents"), ProjectItemsEvents)
Catch objException As Exception
MessageBox.Show(objException.ToString)
End Try
End Sub
Private Sub ProjectItemsEvents_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _
Handles m_objVBProjectItemsEvents.ItemAdded, m_objCSharpProjectItemsEvents.ItemAdded
MessageBox.Show(ProjectItem.Name & " added to the project " & _
ProjectItem.ContainingProject.Name)
End Sub
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
End Sub
End Class
The extensibility model of Visual Studio 2005 allows to use the same approach seen so far for Visual Studio .NET 2002/2003, but it also provides a new EnvDTE80.Events2 class which has the ProjectsEvents and ProjectItemsEvents properties to directly return the events, which fire for any .NET language. The following add-in sample (VB.NET 2005) shows how to show a message box each time that a file (any language) is added to a project: Private m_objDTE As EnvDTE.DTE
Private WithEvents m_objProjectItemsEvents As ProjectItemsEvents
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
Dim objEvents2 As EnvDTE80.Events2
Try
m_objDTE = CType(application, EnvDTE.DTE)
objEvents2 = CType(m_objDTE.Events, EnvDTE80.Events2)
m_objProjectItemsEvents = objEvents2.ProjectItemsEvents
Catch objException As Exception
MessageBox.Show(objException.ToString)
End Try
End Sub
Private Sub ProjectItemsEvents_ItemAdded(ByVal ProjectItem As EnvDTE.ProjectItem) _
Handles m_objProjectItemsEvents.ItemAdded
MessageBox.Show(ProjectItem.Name & " added to the project " & _
ProjectItem.ContainingProject.Name)
End Sub
Go back to the 'Resources for Visual Studio .NET extensibility' section for more articles like this
|
|||||||||||||||||||||||||||||||||||||||||||||||||