Как создать расширение для VS2012 с помощью пакета VS в событии сохранения файла

Мне нужно создать расширение для выполнения некоторых пользовательских действий в событии сохранения файла в Visual Studio. Как добиться этого с помощью VS Package.

Я поражен приведенным ниже кодом, что посоветуете?

    #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;
    }
}

}


person Anand Kumar    schedule 14.02.2014    source источник


Ответы (1)


Вы можете подписаться на DTE.Events.DocumentEvents.DocumentSaved событие.

person Sergey Vlasov    schedule 15.02.2014