C# 撤消/重做代码不执行任何操作
对于我的图块编辑器,我有 2 堆 TileMap、撤消和重做。每次用户进行更改时,地图的状态都会添加到堆栈中,然后进行更改。这是我的撤消代码:
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (undo.Count != 0)
{
redo.Push(tileMap);
tileMap = undo.Peek();
undo.Pop();
}
}
但是地图没有改变。为什么?
for my tile editor I have 2 stacks of TileMaps, undo and redo. Every time the user makes a change the state of the map is added to the stack, than the change is made. Here is my undo code:
private void undoToolStripMenuItem_Click(object sender, EventArgs e)
{
if (undo.Count != 0)
{
redo.Push(tileMap);
tileMap = undo.Peek();
undo.Pop();
}
}
The map however does not change. Why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果撤消和重做只是 Stack 对象,那么什么都不会发生。相反,您需要添加代码以在发生撤消时实际更新 UI。在这种情况下,我想您将使用从堆栈中弹出的当前tileMap 重新绘制地图。
If undo and redo are just Stack objects, then nothing should happen. Instead, you need to add code to actually update the UI when an undo takes place. In this case, I imagine you would redraw the map using the current tileMap as popped off the stack.
我知道这是一个旧线程,但我刚刚遇到了同样的问题。事实证明,我传递给 CurrentItem 的项目与我在内存中编辑的项目相同,因此它只是前后传递相同的对象。
我通过在对象上创建一个 Clone 函数以返回其自身的新版本来解决这个问题,并且每次执行撤消、重做或 additem 函数时都会执行此操作(双向)。这样,它总是消除对正在编辑的对象的依赖。现在一切正常了:)
I know this is an old thread, but I have just encountered this very same issue. Turned out the item I was passing to CurrentItem was the same item I was editing in memory, so it was just passing the same object backwards and forwards.
I got round this by creating a Clone function on the object to return a new version of itself, and I did this every time the undo, redo, or additem functions were performed, in both directions. That way, it was always removing the dependency on the object being edited. It's now all working :)
如果不是 UI 更新问题...
您的重做和撤消引用是否是不同的堆栈对象?如果它们都引用相同的堆栈对象,那么您的代码只会压入并弹出当前状态。
或者类似地,您是否将另一个引用推送到同一个tileMap对象或其副本? (即您可能想要
Push(tileMap.DeepCopy())
)另外,为什么您要先Peek,然后再Pop?您可以直接 Pop 到tileMap 中。
If it's not a UI update issue...
Are your redo and undo references to different stack objects? If they both reference the same stack object, then your code would just push-and-pop the current state.
Or similarly, are you pushing another reference to the same tileMap object, or a copy of it? (i.e. You probably want to
Push(tileMap.DeepCopy())
)Also, why do you Peek and then Pop? You can just Pop directly into tileMap.