问题
In my extension I use to tap into the document saved event using DTE:
this._events2 = (Events2)this._dte.Events;
//setup document events
this._documentEvents = this._events2.DocumentEvents;
this._documentEvents.DocumentSaved += _documentEvents_DocumentSaved;
I am migrating my extension to VS 2017 and want to start using the Roslyn stuff instead of DTE. I figured out how to get the Visual Studio Workspace and tap into the workspace changed event. Now I have access to all these document events
    /// <summary>
    /// A document was added to the current solution.
    /// </summary>
    DocumentAdded = 9,
    /// <summary>
    /// A document was removed from the current solution.
    /// </summary>
    DocumentRemoved = 10,
    /// <summary>
    /// A document in the current solution was reloaded.
    /// </summary>
    DocumentReloaded = 11,
    /// <summary>
    /// A document in the current solution was changed.
    /// </summary>
    DocumentChanged = 12,
But there is no DocumentSaved. DocumentChange fires every time a keystroke is made and DocumentReloaded never seems to fire at all. Is it possible to detect just a document saved using the roslyn workspace events?
回答1:
To detect document save event( OnBeforeSave() or OnAfterSave() ) you can implement IVsRunningDocTableEvents3 interface. You can do that by implementing this interface into a helper class and exposing a public event event OnBeforeSaveHandler BeforeSave and a public delegate delegate void OnBeforeSaveHandler(object sender, Document document). 
To catch this event just  : runningDocTableEvents.BeforeSave += OnBeforeSave and then you can write your code in the OnBeforeSave method.
My implementation of the IVsRunningDocTableEvents3 interface look like this:  
public class RunningDocTableEvents : IVsRunningDocTableEvents3
{
  #region Members
  private RunningDocumentTable mRunningDocumentTable;
  private DTE mDte;
  public delegate void OnBeforeSaveHandler(object sender, Document document);
  public event OnBeforeSaveHandler BeforeSave;
  #endregion
  #region Constructor
  public RunningDocTableEvents(Package aPackage)
  {
    mDte = (DTE)Package.GetGlobalService(typeof(DTE));
    mRunningDocumentTable = new RunningDocumentTable(aPackage);
    mRunningDocumentTable.Advise(this);
  }
  #endregion
  #region IVsRunningDocTableEvents3 implementation
  public int OnAfterAttributeChange(uint docCookie, uint grfAttribs)
  {
    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 OnAfterDocumentWindowHide(uint docCookie, IVsWindowFrame pFrame)
  {
    return VSConstants.S_OK;
  }
  public int OnAfterFirstDocumentLock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
  {
    return VSConstants.S_OK;
  }
  public int OnAfterSave(uint docCookie)
  {
    return VSConstants.S_OK;
  }
  public int OnBeforeDocumentWindowShow(uint docCookie, int fFirstShow, IVsWindowFrame pFrame)
  {
    return VSConstants.S_OK;
  }
  public int OnBeforeLastDocumentUnlock(uint docCookie, uint dwRDTLockType, uint dwReadLocksRemaining, uint dwEditLocksRemaining)
  {
    return VSConstants.S_OK;
  }
  public int OnBeforeSave(uint docCookie)
  {
    if (null == BeforeSave)
      return VSConstants.S_OK;
    var document = FindDocumentByCookie(docCookie);
    if (null == document)
      return VSConstants.S_OK;
    BeforeSave(this, FindDocumentByCookie(docCookie));
    return VSConstants.S_OK;
  }
  #endregion
  #region Private Methods
  private Document FindDocumentByCookie(uint docCookie)
  {
    var documentInfo = mRunningDocumentTable.GetDocumentInfo(docCookie);
    return mDte.Documents.Cast<Document>().FirstOrDefault(doc => doc.FullName == documentInfo.Moniker);
  }
  #endregion
}
I have used this implementation to format some documents when any kind of save command (CTRL + S, Save All, Compile, Build etc) from VS was triggered.
If you want to get the save event from a certain command like Compile you must add a check in your OnBeforeSave() method add some more code
First you must to keep a strong reference to CommandEvents var vommandEvents = dte.Events.CommandEvents and add a new method to the CommandEvents commandEvents.BeforeExecute += CommandEventsBeforeExecute;.
This will work because the CommandsEvents will always be called before the BeforeSave. This is how things works in Visual Studio, every action represents a command which requires some steps and events (Eg. Compile command contains in its workflow, before of anything, the save document event).
public override void OnBeforeSave(object sender, Document aDocument)
{
  if (false == myCompileCommandFlag)
    return;
  // write your code here 
}
public void CommandEventsBeforeExecute(string aGuid, int aId, object aCustomIn, object aCustomOut, ref bool aCancelDefault)
{
  string commandName = GetCommandName(aGuid, aId);
  if (0 != string.Compare("Build.Compile", commandName))
  {
    return;
  }
  myCompileCommandFlag= true;
}
public string GetCommandName(string aGuid, int aId)
{
  if (null == aGuid)
    return string.Empty;
  if (null == mCommand)
    return string.Empty;
  Command cmd = mCommand.Item(aGuid, aId);
  if (null == cmd)
    return string.Empty;
  return cmd.Name;
}
    回答2:
You have DTE2 interface and there you have Events.DocumentEvents.DocumentSaved
I didn't try it but its look promising.
来源:https://stackoverflow.com/questions/41620241/can-i-detect-document-saved-not-changed-with-visual-studio-workspace-in-a-vsix