想要 Actions 列表中的下一个 Stack<T>
Wants next Stack<T> from the list of Actions
我正在为我的应用程序制作 UndoRedo Functionlity。现在在函数 DoAction
我有在 Stack<UndoRedoAction>
采取的行动列表 现在我想得到最后采取的行动这将自动成为列表中的第一个要取出列表中的第一个我使用了 actionList.Peek();
现在出现的情况是下次我想拿第二个从列表中。我不知道该怎么做
private void DoAction(Stack<UndoRedoAction> actionList, Stack<UndoRedoAction> storeList, Action<UndoRedoAction> action)
{
if (actionList.Count > 0)
{
UndoRedoAction urAction = actionList.Peek();
action(urAction);
storeList.Push(urAction);
}
}
您需要使用 Stack<T>.Pop
而不是 Peek
。 Pop
删除堆栈中最后添加的项目并 returns 它而 Peek
returns 最后添加的项目而不将其从堆栈中删除。
一定要使用 Pop(如@Matias 所说)。
另请查看 Command Pattern,因为它非常适合在实际执行工作的代码之间进行抽象,并为撤消提供了良好的基础。
我正在为我的应用程序制作 UndoRedo Functionlity。现在在函数 DoAction
我有在 Stack<UndoRedoAction>
采取的行动列表 现在我想得到最后采取的行动这将自动成为列表中的第一个要取出列表中的第一个我使用了 actionList.Peek();
现在出现的情况是下次我想拿第二个从列表中。我不知道该怎么做
private void DoAction(Stack<UndoRedoAction> actionList, Stack<UndoRedoAction> storeList, Action<UndoRedoAction> action)
{
if (actionList.Count > 0)
{
UndoRedoAction urAction = actionList.Peek();
action(urAction);
storeList.Push(urAction);
}
}
您需要使用 Stack<T>.Pop
而不是 Peek
。 Pop
删除堆栈中最后添加的项目并 returns 它而 Peek
returns 最后添加的项目而不将其从堆栈中删除。
一定要使用 Pop(如@Matias 所说)。
另请查看 Command Pattern,因为它非常适合在实际执行工作的代码之间进行抽象,并为撤消提供了良好的基础。