How to intercept the project save event on vssdk?

Asked

Viewed 28 times

1

I am currently developing an extension of visual studio 2017 where I need to know when any changes in the project are persisted.

E.g.: When I add a new reference to the project (I know there are events for when the reference is added/changed/removed, but it did not meet my need), the project is marked as a pending to be saved. I need to intercept when it is saved (better if it is before saving)

I tried the events Dte.Events.DocumentEvents.DocumentSaved, but it is not shot in the project save; DTE.Events.SolutionEvents and DTE.Events.SolutionItemEvents have no event of the kind I need

That’s possible?

2 answers

0

According to this link from MSDN, you need to create object references Events and DocumentEvents, otherwise they will be collected by Garbage Collectorand will be rendered useless.

Example:

private EnvDTE.Events events;
private EnvDTE.DocumentEvents documentEvents;

MeuConstrutor()
{
    events = DTE.Events;
    documentEvents = events.DocumentEvents;
    documentEvents.DocumentSaved += OnDocumentSaved; // <- Adicione seu event handler aqui.
}

0


What worked for me like a glove was to implement the interface IVsRunningDocTableEvents3 overriding the method of OnBeforeSave.

That way I knew exactly when the project was to be saved and carry out the actions I needed.

Ex.:

uint cookie;
var runningDocumentTable = (IVsRunningDocumentTable)GetGlobalService(typeof(SVsRunningDocumentTable));

runningDocumentTable.AdviseRunningDocTableEvents(new RunningDocTableEventsHandler(), out cookie);
using Microsoft.VisualStudio;
using Microsoft.VisualStudio.Shell;
using Microsoft.VisualStudio.Shell.Interop;

namespace YourProject
{
    internal class RunningDocTableEventsHandler : IVsRunningDocTableEvents3
    {

        #region Methods

        public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterSave(uint docCookie)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
        {
            return VSConstants.S_OK;
        }

        public int OnAfterAttributeChangeEx(uint docCookie, uint grfAttribs, IVsHierarchy pHierOld, uint itemidOld, string pszMkDocumentOld, IVsHierarchy pHierNew, uint itemidNew, string pszMkDocumentNew)
        {
            return VSConstants.S_OK;
        }

        public int OnBeforeSave(uint docCookie)
        {
            /////// MY CODE ////////
            return VSConstants.S_OK;
        }

        #endregion Methods
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.