C# UWP 撤消按钮

C# UWP Undo button

好的,所以我创建了一个应用程序来管理 2 个团队的分数。 APP Layout。如您所见,我有 A 队和 B 队,下面的 0 添加了总分的分数,而在它下面,您有每轮获得的分数的历史记录。 当您按下开始按钮时,来自 2 个文本框的分数将对所有分数进行加法运算,并将该轮的分数添加到列表中。 如您所见,我创建了一个撤消按钮。因此,例如,如果我不小心按下了 Go 按钮,我只需点击我的撤消按钮即可撤消按钮以撤消我的错误。问题是我不知道在撤消按钮的点击事件中要写什么代码。

private void Undo_Click(object sender, RoutedEventArgs e)
    {

    }

注意:我的列表绑定到我创建的 class。所以每个列表显示它通过 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);
    }

非常感谢在下面回答我问题的两个人,通过阅读他们并尝试执行他们,我找到了我的解决方案!!!! :)

我的意思是:

class List
{
    public int ListA { get; set; }
    public int ListB { get; set; }
} 

然后你创建了 2 个对象 class 列出一个要操作的对象,另一个要像以前的状态一样保持(但它会复制你的对象!!!)

按钮将是:

private void Undo_Click(object sender, RoutedEventArgs e)
    {
        ClassListObj1.ListA = ClassListObj2.ListA;
        ClassListObj1.ListB = ClassListObj2.ListB;
    }

甚至,但我不确定...

private void Undo_Click(object sender, RoutedEventArgs e)
    {
        ClassListObj1 = ClassListObj2;
    }

请记住,在修改列表之前,您必须执行 ClassListObj2.ListA = ClassListObj1.ListA;ClassListObj2.ListB = ClassListObj1.ListB;

结构体的想法更好,但它需要我更多地了解你的应用程序,才能详细说明。

全景, 您可以创建一个 List<List<List>>(对不起,您的 class 名称没有帮助),每次用户添加分数时,您都会像状态一样将值保存在列表中。因此,当您单击撤消时,您会从列表列表中弹出最后一项并替换为您的列表。这是一个例子

List<List<Score>> ScoreStates = new List<List<Score>>();
List<Score> Scores = new List<Score>();

private void Undo_Click(object sender, RoutedEventArgs e)
{
    var lastState = ScoreStates.Last();
    ScoreStates.Remove(lastState);
    Scores = lastState;
}

public class Score
{
    public int TeamA { get; set; }
    public int TeamB { get; set; }
}