Tornado
Tornado

Reputation: 35

about save data in the storage wp 8.1

I created this code in same 3 page 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));
    }
}

}

when click button and go to p2 [point = 5, level = 1] and directly click button and go to p3 output [point = 10 , level = 2] But when I go to page 4 outputs remain such as P3, why?

Upvotes: 1

Views: 46

Answers (1)

Mostafiz Rahman
Mostafiz Rahman

Reputation: 8522

Your output of page-4 is exactly what is supposed to be.

In fact, all page that are navigated afterward will have point = 10 and level = 2 according to your code/logic.

The reason is simple, when the constructor is called both point and level are set to 0. In Button_Click function you have increased their value by 5 and 1 respectively. So the updated values are 5 and 1. Now you have put the point + 5 and level + 1 in UserDatas. So point will always be 10 and level be 2.

Note: If you are trying to increase point by 5 and level by 1 for every page navigation just replace your Button_Clink function with following snippet:

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

Upvotes: 1

Related Questions