0

我需要在Visual Studio中的File Save Event上創建一個擴展來執行一些自定義活動。如何使用VS Package實現這一點。如何在文件保存事件中使用VS Package創建VS2012的擴展

我感到下面的代碼,任何意見?

#region Package Members 

    FileEventsListener listener = null; 

    protected override void Initialize() 
    { 
     Debug.WriteLine (string.Format(CultureInfo.CurrentCulture, "Entering Initialize() of: {0}", this.ToString())); 
     base.Initialize(); 

     listener = new FileEventsListener(); 

    } 

    #endregion 
} 

public class FileEventsListener : IPersistFileFormat, IDisposable 
{ 
    private IVsSolution solution; 
    private uint solutionEventsCookie; 
    private bool isDirty; 
    private string fileName; 
    private const uint MyFormat = 0; 
    private const string MyExtension = ".edmx"; 

    public FileEventsListener() 
    { 
     //solution = Package.GetGlobalService(typeof(SPersistFileFormat)) as IVsSolution; 

     //if (solution != null) 
     //{ 
     // solution.AdviseSolutionEvents(this, out solutionEventsCookie); 
     //} 
    } 

    #region IDisposable Members 

    public void Dispose() 
    { 

    } 

    #endregion 

    public int GetClassID(out Guid pClassID) 
    { 
     ErrorHandler.ThrowOnFailure(((IPersist)this).GetClassID(out pClassID)); 
     return VSConstants.S_OK; 
    } 

    public int GetCurFile(out string ppszFilename, out uint pnFormatIndex) 
    { 
     pnFormatIndex = MyFormat; 
     ppszFilename = fileName; 
     return VSConstants.S_OK; 
    } 

    public int GetFormatList(out string ppszFormatList) 
    { 
     char Endline = (char)'\n'; 
     string FormatList = string.Format(CultureInfo.InvariantCulture, "My Editor (*{0}){1}*{0}{1}{1}", MyExtension, Endline); 
     ppszFormatList = FormatList; 
     return VSConstants.S_OK; 
    } 

    public int InitNew(uint nFormatIndex) 
    { 
     if (nFormatIndex != MyFormat) 
     { 
      return VSConstants.E_INVALIDARG; 
     } 
     // until someone change the file, we can consider it not dirty as 
     // the user would be annoyed if we prompt him to save an empty file 
     isDirty = false; 
     return VSConstants.S_OK; 
    } 

    public int IsDirty(out int pfIsDirty) 
    { 
     pfIsDirty = 0; 

     return VSConstants.S_OK; 
    } 

    public int Load(string pszFilename, uint grfMode, int fReadOnly) 
    { 
     fileName = pszFilename; 
     return VSConstants.S_OK; 
    } 

    public int Save(string pszFilename, int fRemember, uint nFormatIndex) 
    { 
     return VSConstants.S_OK; 
    } 

    public int SaveCompleted(string pszFilename) 
    { 
     return VSConstants.S_OK; 
    } 
} 

}

回答

相關問題