I would like to be notified when a solution has been completely loaded. Inspired by this answer I tried implementing IVsSolutionEvents
.
When I load a solution with two C# projects, wait until loading has finished and finally close Visual Studio 2017, the output shows only the following trace messages:
VSTestPackage1: OnAfterOpenProject
VSTestPackage1: OnQueryCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnAfterCloseSolution
Is this the expected behavior? Why is OnAfterOpenSolution
not being invoked?
This is the package implementation:
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(PackageGuidString)]
[SuppressMessage("StyleCop.CSharp.DocumentationRules", "SA1650:ElementDocumentationMustBeSpelledCorrectly",
Justification = "pkgdef, VS and vsixmanifest are valid VS terms")]
[ProvideAutoLoad(VSConstants.UICONTEXT.SolutionHasMultipleProjects_string)]
public sealed class VSPackage1 : Package, IVsSolutionEvents
{
public const string PackageGuidString = "2e655097-9510-4cf8-b9d4-ceeacebbaf3c";
private DTE _dte;
private uint _hSolutionEvents = uint.MaxValue;
private IVsSolution _solution;
/// <summary>
/// Initialization of the package; this method is called right after the package is sited, so this is the place
/// where you can put all the initialization code that rely on services provided by VisualStudio.
/// </summary>
protected override void Initialize()
{
base.Initialize();
_dte = (DTE) GetService(typeof(DTE));
AdviseSolutionEvents();
}
protected override void Dispose(bool disposing)
{
UnadviseSolutionEvents();
base.Dispose(disposing);
}
private void AdviseSolutionEvents()
{
UnadviseSolutionEvents();
_solution = GetService(typeof(SVsSolution)) as IVsSolution;
_solution?.AdviseSolutionEvents(this, out _hSolutionEvents);
}
private void UnadviseSolutionEvents()
{
if (_solution == null) return;
if (_hSolutionEvents != uint.MaxValue)
{
_solution.UnadviseSolutionEvents(_hSolutionEvents);
_hSolutionEvents = uint.MaxValue;
}
_solution = null;
}
#region Implementation of IVsSolutionEvents
int IVsSolutionEvents.OnAfterOpenProject(IVsHierarchy pHierarchy, int fAdded)
{
Trace.WriteLine("OnAfterOpenProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryCloseProject(IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel)
{
Trace.WriteLine("OnQueryCloseProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeCloseProject(IVsHierarchy pHierarchy, int fRemoved)
{
Trace.WriteLine("OnBeforeCloseProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterLoadProject(IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy)
{
Trace.WriteLine("OnAfterLoadProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryUnloadProject(IVsHierarchy pRealHierarchy, ref int pfCancel)
{
Trace.WriteLine("OnQueryUnloadProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeUnloadProject(IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy)
{
Trace.WriteLine("OnBeforeUnloadProject", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
Trace.WriteLine("OnAfterOpenSolution", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnQueryCloseSolution(object pUnkReserved, ref int pfCancel)
{
Trace.WriteLine("OnQueryCloseSolution", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnBeforeCloseSolution(object pUnkReserved)
{
Trace.WriteLine("OnBeforeCloseSolution", "VSTestPackage1");
return VSConstants.S_OK;
}
int IVsSolutionEvents.OnAfterCloseSolution(object pUnkReserved)
{
Trace.WriteLine("OnAfterCloseSolution", "VSTestPackage1");
return VSConstants.S_OK;
}
#endregion
}