在IsolatedStorageSettings中恢复计数

时间:2016-07-15 21:12:42

标签: c# silverlight windows-phone-8.1 windows-phone

当我按下按钮完全计算数字时,但当你退出应用程序并返回计数时会再次开始计数,而不是在IsolatedStorageSettings中保存的数字!! 如何计算IsolatedStorageSettins中保存的数字? (我使用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();
    }
}

1 个答案:

答案 0 :(得分:0)

根据您的代码Points将始终在页面加载时初始化为0,当您点击计数时,它将从0开始递增。您需要从appsettings加载计数并将其放入Points < / p>

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();            
    }

}