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

16

今天我发现FW 4.5有自己的撤消/重做管理器(如果我理解正确的话)http://msdn.microsoft.com/en-us/library/System.ComponentModel.Design.UndoEngine%28v=vs.110%29.aspx

嗯,我找不到任何关于如何开始使用这个类来制作基于文本控件的简单撤消/重做的示例,我知道其他可用于执行撤消操作的方法,但是我只想学习如何使用这个功能。

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

有人能为我们提供C#VBNET的示例吗?(如果可能的话,请提供VBNET文档)


1
一些方法,例如AddUndoUnit,让人感觉它就像一个框架(即你仍然需要添加很多东西,如检测更改-这是困难的部分),而不是Etienne的方法。如果你认为Etienne的方法很复杂,请看看UndoUnit的文档:http://msdn.microsoft.com/en-us/library/system.componentmodel.design.undoengine.undounit(v=vs.110).aspx - Ňɏssa Pøngjǣrdenlarp
你需要自己实现。UndoEngine 是抽象的,需要派生自它。此外,它仅在 DesignTime 期间有用,当你构建自己的组件并希望提供撤消/重做功能时(而不是运行时),因为它依赖于仅在 DesignTime 可用的服务。此外,自 .NET Framework 2.0 开始就有这个类。 - Jehof
UndoEngine的临时使用相当不寻常,因为标准设计主机(即Visual Studio)甚至支持自定义控件的大多数撤消操作。你的情况是什么? - Simon Mourier
@Simon Mourier,我的情况是任何情况。正如我在问题中所说,并且我在赏金评论中也指出,我只想学习例如使用TextBox的用法。感谢您的评论。 - ElektroStudios
2
你的场景没有解释你想用撤销引擎做什么。即使是从基本文本控件派生出来的文本控件已经可以进行撤销/重做操作,而不需要任何花哨的东西。 - Simon Mourier
这个类是为控件的设计时支持而设计的,不适用于运行时。 - John Saunders
3个回答

12

UndoEngine是一个抽象类,Visual Studio和设计师在各自的方式中实现了UndoEngine,并且这些实现必须是私有的或不可重新分发的。你将无法使用它,实际上抽象类只是一个具有少量实现的接口,它根本不是一个撤销框架。

尽管如此,仍然需要编写自己的撤销管理,但是从UndoEngine类派生你的撤销引擎的好处是,它可以轻松地与VS和其他基于MS的编辑器集成。

  1. 如果你想在Visual Studio文档编辑器内提供编辑体验,那么你必须从UndoEngine派生你的Undo框架类,VS将自动突出显示禁用的撤消/重做按钮,并调用你的类上的undo/redo方法。
  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);
            }
    }

3
如果您的问题是如何在运行时使用它,那么答案在MSDN中:(链接)。它指定了在设计时的通用撤销/重做功能。因此我怀疑它在运行时很难使用。
如果您想要一个利用这个类的自定义用户控件的示例,我找不到任何内容,抱歉。

我是指最后一个问题,我的意思是如何实现它,无论如何谢谢你! - ElektroStudios

2

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接