I'm developing a language service for Visual Studio through a VSPackage. I need to update my parse data whenever files get added/removed from the solution's projects.
I want to subscribe to solution and project events.
I tried as follows, but none of these events get fired when I add/remove projects to the solution or add/remove items to projects.
DTE dte = (DTE)languageService.GetService(typeof(DTE));
if (dte == null)
return;
((Events2)dte.Events).SolutionEvents.ProjectAdded += SolutionEvents_ProjectAdded;
((Events2)dte.Events).SolutionEvents.ProjectRemoved += SolutionEvents_ProjectRemoved;
((Events2)dte.Events).ProjectItemsEvents.ItemAdded += ProjectItemsEvents_ItemAdded;
((Events2)dte.Events).ProjectItemsEvents.ItemRemoved += ProjectItemsEvents_ItemRemoved;
What's the best way to subscribe to these events from a VSPackage?
DTE Events are a little weird, you need to cache the event source object (SolutionEvents and ProjectItemEvents in your case), so that COM Interop knows to keep them alive.
More on this @ http://msdn.microsoft.com/en-us/library/ms165650(v=vs.80).aspx