I have been working on creating an Add-In to Visual Studio. One of the things I was trying to do is hooking to several solution related events such as Solution Opened and Solution After Closing.
The solution opened event Occurs immediately after opening a solution or project.
The solution after closing event Occurs immediately closing closing a solution.
You can read more about the events here.
So, while trying to connect to the opened event, I wrote this code:
_applicationObject.Events.SolutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(sec_Opened);
This looks fairly simple. The _applicationObject is the DTE2 object that we get in the OnConnection event handler. The problem was that when I opened a solution the event did not fire!
After plaing with it for a while I found out that we need to be careful where and how we declare the event. I solved it by declaring the events as class members. Here is what worked for me:
// Declare the variables at the class scope
private Events2 events;
private SolutionEvents solutionEvents;
// Use it somewhere in code
events = (Events2)_applicationObject.Events;
solutionEvents = events.SolutionEvents;
solutionEvents.Opened += new _dispSolutionEvents_OpenedEventHandler(sec_Opened);