Visual Studio VSIX OnSolutionOpened无法正常工作

Leo*_*oue 2 visual-studio vsix visual-studio-extensions

我正在尝试为Visual Studio实现某种起始页扩展。主要目的是通过每次打开解决方案时启动本地HTML文件,为我工作的公司中的特定项目提供指导和最佳实践。我从使用Visual Commander(https://vlasovstudio.com/visual-commander/extensions.html)开始,它非常好用。但是我想改为使其成为VSIX文件。经过一些研究后,我生成了代码,但是如果我调试或直接从debug文件夹安装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
Run Code Online (Sandbox Code Playgroud)

Car*_*ero 5

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