Leonardo Tanoue
Leonardo Tanoue

Reputation: 261

Visual Studio VSIX OnSolutionOpened not working

I'm trying to implement some sort of Start Page Extension for Visual Studio. The main purpose would be to put Instructions and Best Practices for specific projects within the company I work for by launching an local HTML file every time a solution is opened. I started by using the Visual Commander (https://vlasovstudio.com/visual-commander/extensions.html) which worked perfectly. But I wanted to make it a VSIX file instead. After some research I generated the code but if I debug or directly install the vsix from the debug folder nothing happens (not even if I throw an exception on the first line). The code is pretty simple:

 #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

Upvotes: 2

Views: 658

Answers (2)

Carlos Quintero
Carlos Quintero

Reputation: 4414

Don't forget to move the solutionEvents declaration at class level instead of method level, or your next question will be that only works for a while (because of garbage collection). See https://msdn.microsoft.com/en-us/library/envdte.solutionevents.aspx

Upvotes: 7

Sosian
Sosian

Reputation: 622

You need to specify with an attribute above the Initialize() method when VS should load your package.

You probably want this attribute:

[ProvideAutoLoad(VSConstants.UICONTEXT.NoSolution_string)]

For a list of all possible load attributes visit: https://www.mztools.com/articles/2013/MZ2013027.aspx

Upvotes: 1

Related Questions