/// <summary> /// Initialization of the package; this method is called right after the package is sited. /// </summary> protected override async System.Threading.Tasks.Task InitializeAsync( CancellationToken cancellationToken, IProgress <ServiceProgressData> progress) { await JoinableTaskFactory.SwitchToMainThreadAsync(cancellationToken); //Log needs the OptionsPage OptionsPage = (OptionsPage)GetDialogPage(typeof(OptionsPage)); Instance = this; //Log needs the dte object dte = GetGlobalService(typeof(DTE)) as DTE; if (dte == null) { //this log will only log with Debug.WriteLine, since we failed to get the DTE for some reason Log("Could not get DTE object. Will not initialize."); return; } //This is the earliest we can safely log (that will go the output window) //previous logs will be Debug.WriteLine only Log($"Entering {nameof(InitializeAsync)}"); var isSlnLoaded = await IsSolutionLoadedAsync(); if (isSlnLoaded) { //already loaded, so we need to handle it asap HandleSolutionOpen(); } //it's recommended to keep refs to Events objects to avoid the GC eating them up //https://stackoverflow.com/a/32600629/2573470 solutionEvents = dte.Events.SolutionEvents; if (solutionEvents == null) { Log("Could not get te.Events.SolutionEvents. Will not initialize."); return; } solutionEvents.Opened += HandleSolutionOpen; solutionEvents.BeforeClosing += HandleSolutionClose; // Add our command handlers for menu (commands must exist in the .vsct file) Log("Adding tool menu handler."); var mcs = await GetServiceAsync(typeof(IMenuCommandService)) as OleMenuCommandService; if (mcs != null) { // Create the command for the menu item. var menuCommandID = new CommandID(GuidList.guidLocalHistoryCmdSet, (int)PkgCmdIDList.cmdidLocalHistoryMenuItem); var menuItem = new MenuCommand(ProjectItemContextMenuHandler, menuCommandID); mcs.AddCommand(menuItem); Log("Added context menu command."); // Create the command for the tool window var toolwndCommandID = new CommandID(GuidList.guidLocalHistoryCmdSet, (int)PkgCmdIDList.cmdidLocalHistoryWindow); var menuToolWin = new MenuCommand(ToolWindowMenuItemHandler, toolwndCommandID); mcs.AddCommand(menuToolWin); Log("Added menu command."); } else { Log("Could not get IMenuCommandService. Tool menu will not work."); } ShowToolWindow(false, cancellationToken); dte.Events.DocumentEvents.DocumentOpened += HandleDocumentOpen; }