c# - 在 Visual Studio 2015 中捕获解决方案事件

标签 c# visual-studio-2015 visual-studio-extensions

我正在开发一个 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 上下文,例如未打开解决方案的上下文。

Source

关于c# - 在 Visual Studio 2015 中捕获解决方案事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36543470/

相关文章:

c# - Azure ServiceBus - 多次读取同一消息

c# - 如何从托管 Web 浏览器控件的应用程序调用 Web 浏览器控件查看的页面中的 JavaScript 函数?

c# - EF6 postgresql 数据库优先,无法生成模型

google-chrome - Cassini/Google Chrome TypeScript 调试问题

javascript - Visual Studio 2015 package.appxmanifest 中起始页中的 ms-appx-web 存在错误?

c# - 如何在 ZedGraph 中同步三个 GraphPane?

c# - 无法使用ajax jquery post上传大型csv文件| ASP MVC

c# - 如何使用 Visual Studio 扩展从当前解决方案中收集类型?

c# - 如何使用 Roslyn 否定 ExpressionSyntax

visual-studio - Visual Studio : Custom Syntax Coloring