public async Task InitializeAsync()
        {
            if (!initialized)
            {
                this.appObject = await package.GetServiceAsync <SDTE, DTE>();

                // Setup solution related stuff
                this.solutionService = await package.GetServiceAsync <SVsSolution, IVsSolution2>();

                this.solutionBuildService = await package.GetServiceAsync <SVsSolutionBuildManager, IVsSolutionBuildManager2>();

                this.selectionMonitor = await package.GetServiceAsync <SVsShellMonitorSelection, IVsMonitorSelection>();

                // Following code needs to be executed on main thread
                await package.JoinableTaskFactory.SwitchToMainThreadAsync();

                // Set startup project
                ErrorHandler.ThrowOnFailure(this.solutionService.AdviseSolutionEvents(this, out solutionEventsCookie));
                ErrorHandler.ThrowOnFailure(this.selectionMonitor.AdviseSelectionEvents(this, out selectionEventsCookie));
                ErrorHandler.ThrowOnFailure(this.solutionBuildService.AdviseUpdateSolutionEvents(this, out updateSolutionEventsCookie));

                commandEvents = this.appObject.Events.CommandEvents;
                commandEvents.BeforeExecute += CommandEventsOnBeforeExecute;

                if (IsSolutionOpen)
                {
                    foreach (var pHierarchy in GetSupportedProjects())
                    {
                        Guid   projectGuid = pHierarchy.GetGuid();
                        string projectDir  = pHierarchy.GetProjectDir();
                        string projectName = pHierarchy.GetName();

                        ProjectStateMap[projectGuid] = new ProjectState {
                            ProjectDir = projectDir, ProjectName = projectName, IsLoaded = true
                        };
                    }
                }

                initialized = true;
            }
        }
예제 #2
0
        /// <summary>
        /// Initializes the singleton instance of the command.
        /// </summary>
        /// <param name="package">Owner package, not null.</param>
        public static async Task InitializeAsync(CmdArgsPackage package)
        {
            if (package == null)
            {
                throw new ArgumentNullException(nameof(package));
            }

            var cmdService = await package.GetServiceAsync <IMenuCommandService, OleMenuCommandService>();

            // AddCommand needs to be run on main thread!
            await package.JoinableTaskFactory.SwitchToMainThreadAsync();

            Instance = new Commands(package, cmdService);
        }