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?
Alternatively you could use IVsSolutionEvents3, which has much better events
[PackageRegistration( UseManagedResourcesOnly = true )]
[InstalledProductRegistration( "#110", "#112", "1.0", IconResourceID = 400 )]
// add these 2 Annotations to execute Initialize() immediately when a project is loaded
[ProvideAutoLoad( VSConstants.UICONTEXT.SolutionHasSingleProject_string )]
[ProvideAutoLoad( VSConstants.UICONTEXT.SolutionHasMultipleProjects_string )]
[Guid( GuidList.XYZ )]
public sealed class UnityProjectUpdateHandlerPackage : Package, IVsSolutionEvents3
{
private DTE _dte;
private IVsSolution solution = null;
private uint _hSolutionEvents = uint.MaxValue;
protected override void Initialize()
{
Trace.WriteLine( string.Format( CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString() ) );
base.Initialize();
this._dte = (DTE) this.GetService( typeof( DTE ) );
AdviseSolutionEvents();
}
protected override void Dispose( bool disposing )
{
UnadviseSolutionEvents();
base.Dispose( disposing );
}
private void AdviseSolutionEvents()
{
UnadviseSolutionEvents();
solution = this.GetService( typeof( SVsSolution ) ) as IVsSolution;
if ( solution != null )
{
solution.AdviseSolutionEvents( this, out _hSolutionEvents );
}
}
private void UnadviseSolutionEvents()
{
if ( solution != null )
{
if ( _hSolutionEvents != uint.MaxValue )
{
solution.UnadviseSolutionEvents( _hSolutionEvents );
_hSolutionEvents = uint.MaxValue;
}
solution = null;
}
}
private Project[] GetProjects()
{
return _dte.Solution.Projects
.Cast<Project>()
.Select( x => ( (VSProject) x.Object ).Project )
.ToArray();
}
public int OnAfterLoadProject( IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy )
{
// Do something
return VSConstants.S_OK;
}
public int OnAfterOpenSolution( object pUnkReserved, int fNewSolution )
{
foreach ( var project in GetProjects() )
; // Do something
return VSConstants.S_OK;
}
public int OnBeforeUnloadProject( IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy )
{
// Do something
return VSConstants.S_OK;
}
public int OnAfterCloseSolution( object pUnkReserved )
{ return VSConstants.S_OK; }
public int OnAfterClosingChildren( IVsHierarchy pHierarchy )
{ return VSConstants.S_OK; }
public int OnAfterMergeSolution( object pUnkReserved )
{ return VSConstants.S_OK; }
public int OnAfterOpenProject( IVsHierarchy pHierarchy, int fAdded )
{ return VSConstants.S_OK; }
public int OnAfterOpeningChildren( IVsHierarchy pHierarchy )
{ return VSConstants.S_OK; }
public int OnBeforeCloseProject( IVsHierarchy pHierarchy, int fRemoved )
{ return VSConstants.S_OK; }
public int OnBeforeClosingChildren( IVsHierarchy pHierarchy )
{ return VSConstants.S_OK; }
public int OnBeforeOpeningChildren( IVsHierarchy pHierarchy )
{ return VSConstants.S_OK; }
public int OnBeforeCloseSolution( object pUnkReserved )
{ return VSConstants.S_OK; }
public int OnQueryCloseProject( IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel )
{ return VSConstants.S_OK; }
public int OnQueryCloseSolution( object pUnkReserved, ref int pfCancel )
{ return VSConstants.S_OK; }
public int OnQueryUnloadProject( IVsHierarchy pRealHierarchy, ref int pfCancel )
{ return VSConstants.S_OK; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With