或者,您可以使用 IVsSolutionEvents3,它有更好的事件
[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; }
}