关于在存储wp 8.1中保存数据

本文关键字:保存 数据 wp 存储 | 更新日期: 2023-09-27 18:07:09

我在相同的3页创建了这个代码p1+p2+p3:

public partial class P1 : PhoneApplicationPage
{
    IsolatedStorageSettings UserDatas = IsolatedStorageSettings.ApplicationSettings;
    int point;
    int level;
    public P1()
    {
        InitializeComponent();
        this.Loaded += P1_Loaded;
    }
    private void P1_Loaded(object sender, RoutedEventArgs e)
    {
        if (UserDatas.Contains("Points") && UserDatas.Contains("Levels"))
        {
            PBlock.Text = "Point: " + UserDatas["Points"];
            LBlock.Text = "Levels: " + UserDatas["Levels"];
        }
    }
    private void Button_Click(object sender, RoutedEventArgs e)
    {
        point = point + 5;
        level = level + 1;
        UserDatas["Points"] = point + 5;
        UserDatas["Levels"] = level + 1;
        NavigationService.Navigate(new Uri("/Views/P2.xaml", UriKind.Relative));
    }
}

}

当点击按钮进入p2 [point = 5, level = 1]直接点击按钮,进入p3输出【点数= 10,等级= 2】但是当我转到第4页时,输出仍然是P3,为什么?

关于在存储wp 8.1中保存数据

您的第4页的输出正是应该是什么。

事实上,根据你的代码/逻辑,之后导航的所有页面都有point = 10level = 2

原因很简单,当调用构造函数时,pointlevel都被设置为0。在Button_Click函数中,您分别将其值增加了5和1。所以更新后的值是5和1。现在你已经把point + 5level + 1放在UserDatas中。所以point永远是10,level永远是2。

注意:如果您试图为每个页面导航增加5点和1级,只需将Button_Clink函数替换为以下代码片段:

private void Button_Click(object sender, RoutedEventArgs e)
{
    UserDatas["Points"] = UserDatas["Points"] + 5;
    UserDatas["Levels"] = UserDatas["Levels"] + 1;
    NavigationService.Navigate(new Uri("/Views/P2.xaml", UriKind.Relative));
}