隔离存储设置中的恢复计数

本文关键字:恢复 存储 设置 隔离 | 更新日期: 2023-09-27 17:57:24

当我按下按钮时,可以完美地计数数字,但是当您退出应用程序并返回计数时,计数再次开始计数,而不是在隔离存储设置中保存的数字时!!当计算保存在隔离存储设置中的数字时,我该如何做到这一点?(我使用Windows Phone 8.1 silverlight)

IsolatedStorageSettings setting = IsolatedStorageSettings.ApplicationSettings;
    int Points;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        this.Loaded += Page2_Loaded;
    }
    private void Page2_Loaded(object sender, RoutedEventArgs e)
    {
        if (setting.Contains("save"))
        {
            PointsText.Text = setting["save"].ToString();
        }
    }
    private void Counts_Click(object sender, RoutedEventArgs e)
    {
        Points = Points + 1;
        setting["save"] = Points;
        PointsText.Text = setting["save"].ToString();
    }
}

隔离存储设置中的恢复计数

根据您的

代码,当页面加载时,Points将始终初始化为0,当您单击计数时,它将从0递增。您需要从应用程序设置中加载计数并将其放入Points

private void Page2_Loaded(object sender, RoutedEventArgs e)
{
    if (setting.Contains("save"))
    {
        //Initialize Points with the value from settings
        Points = int.Parse(setting["save"].ToString());
        PointsText.Text = Points.ToString();            
    }
}