【发布时间】:2016-06-22 17:15:59
【问题描述】:
好的,所以我创建了一个应用来管理 2 个团队的分数。 APP Layout。正如你所看到的,我有 A 队和 B 队,下面的 0 增加了总分的分数,而在下面你有每轮获得的分数的历史记录。 当您按下 go 按钮时,来自 2 个文本框的点会为所有分数进行加法,并将该轮的分数添加到列表中。 如您所见,我创建了一个撤消按钮。因此,例如,如果我不小心按了“开始”按钮,我只需点击我的撤消按钮即可撤消按钮来撤消我的错误。问题是我不知道在撤销按钮的点击事件中写什么代码。
private void Undo_Click(object sender, RoutedEventArgs e)
{
}
注意:我的列表绑定到我创建的类。所以每个列表都会通过 observablecollection 显示它需要的属性。
class List
{
public int ListA { get; set; }
public int ListB { get; set; }
}
更新:
private void Undo_Click(object sender, RoutedEventArgs e)
{
var lastState = Lists.Last();
int teamAScore, teamBScore, listA, listB;
// this way i got the Active scores.
int.TryParse(CurrentScoreA.Text, out teamAScore);
int.TryParse(CurrentScoreB.Text, out teamBScore);
// this way i got the last score that i want to remove.
listA = lastState.ListA;
listB = lastState.ListB;
// here i remove the last score from the Active one.
teamAScore = teamAScore - listA;
teamBScore = teamBScore - listB;
// And here i replace my Active score with
// the new one that has the last states removed.
CurrentScoreA.Text = teamAScore.ToString();
CurrentScoreB.Text = teamBScore.ToString();
// this one removes the last states of the list
// so this way i can always remove the last part of my lsit
// from both my active score and list till i go back to 0.
Lists.Remove(lastState);
}
非常感谢下面回答我问题的 2 个人,通过阅读并尝试执行它们,我找到了我的解决方案!!!! :)
【问题讨论】:
-
理论上,您只需在单击处理程序中添加一行,记录执行的操作(甚至执行的操作的运行队列)。单击撤消时,您只需反转存储的操作。
-
撤消应该将所有项目返回到以前的状态...这可以通过两种方式完成(可能更多):1.保持列表的先前状态,将列表复制到临时列表并在进行任何更改之前将它们保存到其中……但这可能会占用大量资源。 2. 保留一个 las mod 变量,允许您在列表上应用相反的操作,例如包含所有可能更改的结构更改,并存储最后一个完成的操作。
标签: c# win-universal-app undo