当解决方案完全加载时,我希望能收到通知。受这个答案启发,我尝试实现IVsSolutionEvents
。
当我加载一个包含两个C#项目的解决方案,并等待加载完成后最终关闭Visual Studio 2017,输出只显示以下跟踪消息:
VSTestPackage1: OnAfterOpenProject
VSTestPackage1: OnQueryCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseSolution
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnQueryCloseProject
VSTestPackage1: OnBeforeCloseProject
VSTestPackage1: OnAfterCloseSolution
这是期望的行为吗?为什么没有调用OnAfterOpenSolution
?
以下是包的实现:
[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
}
[ProvideAutoLoad]
参数对解决方案事件的影响。感谢您的提示。 - CodeFox