如何开始使用.NET Framework UndoEngine类?

Ele*_*ios 16 .net c# vb.net undo-redo winforms

今天我发现FW 4.5有他们自己的undoredo经理(如果我理解的话)http://msdn.microsoft.com/en-us/library/System.ComponentModel.Design.UndoEngine%28v=vs.110% 29.aspx

好吧,我找不到任何关于如何开始使用这个类只是为了对基于文本的控件进行简单的撤销/重做的示例,我知道其他替代方法可以做出可撤销的事情,但我只是想学习如何使用它.

当我尝试使用构造函数时,它有任何参数要传递,而且Intellisense也没有向我显示System.ComponentModel.Design.UndoEngine该类的任何方法,我真的不知道如何使用它.

有人可能会说明我们有一个例子C#VBNET?(如果可能请提供VBNET文档)

Aka*_*ava 11

UndoEngine是一个抽象类,Visual Studio和Designers以自己的方式实现了UndoEngine,这些必须是私有的或不可用于重新分发.您将无法使用它,实际上抽象类只是一个实现很少的接口,它根本不是一个撤销框架.

您仍然必须编写自己的撤消管理,但是从UndoEngine类派生撤消引擎的好处是,它可以很容易地与VS和其他基于MS的编辑器托管/集成.

  1. 如果您想在Visual Studio文档编辑器中提供编辑体验,那么您必须从UndoEngine派生您的Undo框架类,VS将自动突出显示禁用撤消/重做按钮,并将在您的类上调用撤消/重做方法.
  2. 如果你想在自己的应用程序中使用UndoEngine,UndoEngine将不会帮助你做任何事情,你必须自己编写每个功能.UndoEngine只管理Undo/Redo堆栈,实际工作在UndoUnit内部.它基于工作单元概念,您的每个操作实际上应该代表可以撤消的工作.

最简单的UndoEngine实现

让我们说你正在改变一个全局变量,

// following code uses big UndoUnit

public void SetGlobalVariable(object v){
    var oldValue = GlobalVariable;

    GlobalVariable = v;

    var action = new UndoUnit{
        UndoAction = ()=>{
            GlobalVariable = oldValue;
        },
        RedoAction = ()=>{
            GlobalVariable = v;
        }
    };
    UndoManager.Add(action);
}


/// <summary>
/// Used to indicates the designer's status 
/// </summary>
public enum UndoUnitState
{
    Undoing,
    Redoing,
}

/// <summary>
/// A UndoUnitBase can be used as a IOleUndoUnit or just a undo step in 
/// a transaction  
/// </summary>
public class UndoUnitBase : IOleUndoUnit
{
    public Action UndoAction {get;set;}
    public Action RedoAction {get;set;}

    private string name = null;
    private Guid clsid = Guid.Empty;

    private bool inDoAction = false;
    private bool isStillAtTop = true;
    private UndoUnitState unitState = UndoUnitState.Undoing;

    protected UndoUnit UnitState
    {
        get { return unitState; }
        set { unitState = value; }
    }

    /// <summary>
    /// </summary>
    /// <param name="undoManager"></param>
    /// <param name="name"></param>
    internal UndoUnitBase(string name)
    {
        this.name = name;
    }

    ~UndoUnitBase()
    {
    }

    /// <summary>
    /// </summary>
    protected bool InDoAction
    {
        get
        {
            return inDoAction;
        }
    }

    public string UndoName
    {
        get
        {
            return name;
        }
        set
        {
            this.name = value;
        }
    }

    public Guid Clsid
    {
        get { return clsid; }
        set { clsid = value; }
    }

    /// <summary>
    /// This property indicates whether the undo unit is at the top (most recently added to)
    /// the undo stack. This is useful to know when deciding whether undo units for operations
    /// like typing can be coallesced together.
    /// </summary>
    public bool IsStillAtTop
    {
        get { return isStillAtTop; }
    }

    /// <summary>
    /// This function do the actual undo, and then revert the action to be a redo 
    /// </summary>
    /// <returns>objects that should be selected after DoAction</returns>
    protected abstract void DoActionInternal();

    /// <devdoc>
    ///     IOleUndoManager's "Do" action.
    /// </devdoc>
    void IOleUndoUnit.Do(IOleUndoManager oleUndoManager)
    {
        Do(oleUndoManager);
    }

    protected virtual int Do(IOleUndoManager oleUndoManager)
    {
        try
        {
            if(unitState== UndoUnitState.Undoing){
                 UndoAction();
            }else{
                 RedoAction();
            }

            unitState = (unitState == UndoUnitState.Undoing) ? UndoUnitState.Redoing : UndoUnitState.Undoing;
            if (oleUndoManager != null)
                oleUndoManager.Add(this);
            return VSConstants.S_OK;
        }
        catch (COMException e)
        {
            return e.ErrorCode;
        }
        catch
        {
            return VSConstants.E_ABORT;
        }
        finally
        {
        }
    }

    /// <summary>
    /// </summary>
    /// <returns></returns>
    void IOleUndoUnit.GetDescription(out string pBstr)
    {
        pBstr = name;
    }

    /// <summary>
    /// </summary>
    /// <param name="clsid"></param>
    /// <param name="pID"></param>
    /// <returns></returns>
    void IOleUndoUnit.GetUnitType(out Guid pClsid, out int plID)
    {
        pClsid = Clsid;
        plID = 0;
    }

    /// <summary>
    /// </summary>
    void IOleUndoUnit.OnNextAdd()
    {
        // We are no longer the top most undo unit; another one was added.
        isStillAtTop = false;
    }
}

public class MyUndoEngine : UndoEngine, IUndoHandler
    {                
            Stack<UndoEngine.UndoUnit> undoStack = new Stack<UndoEngine.UndoUnit>();
            Stack<UndoEngine.UndoUnit> redoStack = new Stack<UndoEngine.UndoUnit>();

            public ReportDesignerUndoEngine(IServiceProvider provider) : base(provider)
            {
            }

            #region IUndoHandler
            public bool EnableUndo {
                    get {
                            return undoStack.Count > 0;
                    }
            }

            public bool EnableRedo {
                    get {
                            return redoStack.Count > 0;
                    }
            }                

            public void Undo()
            {
                    if (undoStack.Count > 0) {
                            UndoEngine.UndoUnit unit = undoStack.Pop();
                            unit.Undo();
                            redoStack.Push(unit);
                    }
            }

            public void Redo()
            {
                    if (redoStack.Count > 0) {
                            UndoEngine.UndoUnit unit = redoStack.Pop();
                            unit.Undo();
                            undoStack.Push(unit);
                    }
            }
            #endregion

            protected override void AddUndoUnit(UndoEngine.UndoUnit unit)
            {
                    undoStack.Push(unit);
            }
    }
Run Code Online (Sandbox Code Playgroud)