我正在开发一个 Visual Studio 插件。我想捕获打开新解决方案的事件。为了实现这一目标,我实现了 IVsSolutionEvents
接口(interface)并使用 AdviseSolutionEvents()
注册它。但是,当我运行 VS 的测试实例并打开解决方案时,没有调用正确的事件方法。
这是我的代码:
public sealed class MyPackage : Package, IVsSolutionEvents
{
// ...
protected override void Initialize()
{
base.Initialize();
IVsSolution solution = GetService(typeof(SVsSolution)) as IVsSolution;
uint cookie = 0;
solution.AdviseSolutionEvents(this, out cookie);
}
// ...
public int OnAfterOpenSolution(object pUnkReserved, int fNewSolution)
{
MessageBox.Show("Opened a solution!");
return VSConstants.S_OK;
}
}
为什么OnAfterOpenSolution()
从未被调用?
最佳答案
好吧,我明白了。
事实证明,默认情况下,Visual Studio 包不会在启动时加载,以避免消耗内存和 CPU。相反,Visual Studio 在需要时加载它们。因此,事件之前不会被注册。
要更改此行为,必须将 ProvideAutoLoad
属性添加到类定义中:
[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]
public sealed class Command1Package : Package
...
此属性的值是一个带有 Guid 的字符串,用于标识 UI 上下文,例如未打开解决方案的上下文。
关于c# - 在 Visual Studio 2015 中捕获解决方案事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543470/