Visual Studio VSIX OnSolutionOpened 不工作

Visual Studio VSIX OnSolutionOpened not working

我正在尝试为 Visual Studio 实施某种起始页扩展。主要目的是通过在每次打开解决方案时启动本地 HTML 文件,为我工作的公司内的特定项目提供说明和最佳实践。我从使用 Visual Commander (https://vlasovstudio.com/visual-commander/extensions.html) 开始,效果很好。但我想让它成为一个 VSIX 文件。经过一些研究,我生成了代码,但如果我调试或直接从调试文件夹安装 vsix,则什么也不会发生(即使我在第一行抛出异常也不会)。代码非常简单:

 #region Package Members

    DTE DTE;

    /// <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();
        try
        {
            IServiceContainer serviceContainer = this as IServiceContainer;
            DTE = serviceContainer.GetService(typeof(SDTE)) as DTE;
            EnvDTE.Events events = DTE.Events;
            EnvDTE.SolutionEvents solutionEvents = events.SolutionEvents;
            solutionEvents.Opened += OnSolutionOpened;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    private void OnSolutionOpened()
    {
        try
        {
            string startupFile = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(DTE.Solution.FullName), GetSolutionStartPage());
            if (System.IO.File.Exists(startupFile))
            {
                DTE.ItemOperations.Navigate(startupFile);
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

    string GetSolutionStartPage()
    {
        return ((DTE.Solution != null) ? System.IO.Path.GetFileNameWithoutExtension(DTE.Solution.FullName) : "") + ".html";
    }

    #endregion

您需要在 Initialize() 方法上方指定一个属性,以便 VS 加载您的包。

您可能需要此属性:

[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]

有关所有可能的负载属性的列表,请访问: https://www.mztools.com/articles/2013/MZ2013027.aspx

不要忘记将 solutionEvents 声明移动到 class 级别而不是方法级别,否则您的下一个问题将是它只能工作一段时间(因为垃圾回收)。参见 https://msdn.microsoft.com/en-us/library/envdte.solutionevents.aspx