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

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (24)

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

那么,我找不到任何关于如何开始使用这个类的例子来简单地撤销/重做基于文本的控件,我知道其他替代方法来做可撤销的事情,但我想学习如何使用它。

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

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

提问于
用户回答回答于

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);
            }
    }
用户回答回答于

如果你的问题是如何在运行时使用它,那么答案在MSDN中

在设计时指定通用的撤销/重做功能。

所以我怀疑它在运行时很容易使用。

扫码关注云+社区