How do I subscribe to solution and project events from a VSPackage

后端 未结 3 725
梦谈多话
梦谈多话 2020-12-09 04:11

I\'m developing a language service for Visual Studio through a VSPackage. I need to update my parse data whenever files get added/removed from the solution\'s projects.

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-09 04:49

    Alternatively you could use IVsSolutionEvents3, which has much better events

    [PackageRegistration( UseManagedResourcesOnly = true )]
    [InstalledProductRegistration( "#110", "#112", "1.0", IconResourceID = 400 )]
    // add these 2 Annotations to execute Initialize() immediately when a project is loaded
    [ProvideAutoLoad( VSConstants.UICONTEXT.SolutionHasSingleProject_string )]
    [ProvideAutoLoad( VSConstants.UICONTEXT.SolutionHasMultipleProjects_string )]
    [Guid( GuidList.XYZ )]
    public sealed class UnityProjectUpdateHandlerPackage : Package, IVsSolutionEvents3
    {
        private DTE _dte;
        private IVsSolution solution = null;
        private uint _hSolutionEvents = uint.MaxValue;
    
        protected override void Initialize()
        {
            Trace.WriteLine( string.Format( CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString() ) );
            base.Initialize();
    
            this._dte = (DTE) this.GetService( typeof( DTE ) );
    
            AdviseSolutionEvents();
        }
    
        protected override void Dispose( bool disposing )
        {
            UnadviseSolutionEvents();
    
            base.Dispose( disposing );
        }
    
        private void AdviseSolutionEvents()
        {
            UnadviseSolutionEvents();
    
            solution = this.GetService( typeof( SVsSolution ) ) as IVsSolution;
    
            if ( solution != null )
            {
                solution.AdviseSolutionEvents( this, out _hSolutionEvents );
            }
        }
    
        private void UnadviseSolutionEvents()
        {
            if ( solution != null )
            {
                if ( _hSolutionEvents != uint.MaxValue )
                {
                    solution.UnadviseSolutionEvents( _hSolutionEvents );
                    _hSolutionEvents = uint.MaxValue;
                }
    
                solution = null;
            }
        }
    
        private Project[] GetProjects()
        {
            return _dte.Solution.Projects
                .Cast()
                .Select( x => ( (VSProject) x.Object ).Project )
                .ToArray();
        }
    
        public int OnAfterLoadProject( IVsHierarchy pStubHierarchy, IVsHierarchy pRealHierarchy )
        {
            // Do something
            return VSConstants.S_OK;
        }
    
        public int OnAfterOpenSolution( object pUnkReserved, int fNewSolution )
        {
            foreach ( var project in GetProjects() )
                ; // Do something
    
            return VSConstants.S_OK;
        }
    
        public int OnBeforeUnloadProject( IVsHierarchy pRealHierarchy, IVsHierarchy pStubHierarchy )
        {
            // Do something
            return VSConstants.S_OK;
        }
    
        public int OnAfterCloseSolution( object pUnkReserved )
        { return VSConstants.S_OK; }
    
        public int OnAfterClosingChildren( IVsHierarchy pHierarchy )
        { return VSConstants.S_OK; }
    
        public int OnAfterMergeSolution( object pUnkReserved )
        { return VSConstants.S_OK; }
    
        public int OnAfterOpenProject( IVsHierarchy pHierarchy, int fAdded )
        { return VSConstants.S_OK; }
    
        public int OnAfterOpeningChildren( IVsHierarchy pHierarchy )
        { return VSConstants.S_OK; }
    
        public int OnBeforeCloseProject( IVsHierarchy pHierarchy, int fRemoved )
        { return VSConstants.S_OK; }
    
        public int OnBeforeClosingChildren( IVsHierarchy pHierarchy )
        { return VSConstants.S_OK; }
    
        public int OnBeforeOpeningChildren( IVsHierarchy pHierarchy )
        { return VSConstants.S_OK; }
    
        public int OnBeforeCloseSolution( object pUnkReserved )
        { return VSConstants.S_OK; }
    
        public int OnQueryCloseProject( IVsHierarchy pHierarchy, int fRemoving, ref int pfCancel )
        { return VSConstants.S_OK; }
    
        public int OnQueryCloseSolution( object pUnkReserved, ref int pfCancel )
        { return VSConstants.S_OK; }
    
        public int OnQueryUnloadProject( IVsHierarchy pRealHierarchy, ref int pfCancel )
        { return VSConstants.S_OK; }
    }
    

提交回复
热议问题