简体   繁体   中英

How to keep textfield data when navigating away and back to page

I am writing a golfing application. Each person enters his score in a textbox for each hole and then navigate to another page optionally. The problem I am having is that I want the numbers to stay there when I click to another page and then come back to that page. How can this be done?

This is my code for my textboxes and textblocks:

private void Calculate_Click(object sender, RoutedEventArgs e)
{

    int x1 = 0;
    Int32.TryParse(textBox1.Text, out x1);
    int x2 = 0;
    Int32.TryParse(textBox2.Text, out x2);
    int x3 = 0;
    Int32.TryParse(textBox3.Text, out x3);
    int x4 = 0;
    Int32.TryParse(textBox4.Text, out x4);
    int x5 = 0;
    Int32.TryParse(textBox5.Text, out x5);
    int x6= 0;
    Int32.TryParse(textBox6.Text, out x6);
    int x7 = 0;
    Int32.TryParse(textBox7.Text, out x7);
    int x8 = 0;
    Int32.TryParse(textBox8.Text, out x8);
    int x9 = 0;
    Int32.TryParse(textBox9.Text, out x9);


    int[] totalScore = new int[10];
    totalScore[0] = x1;
    totalScore[1] = x2;
    totalScore[2] = x3;
    totalScore[3] = x4;
    totalScore[4] = x5;
    totalScore[5] = x6;
    totalScore[6] = x7;
    totalScore[7] = x8;
    totalScore[8] = x9;
    int sum = totalScore.Sum();
    TotalBlock.Text = sum.ToString();


    }

A page is instantiated when you navigate to it (assuming you are running on Silverlight). When you navigate away from it the state will be saved in a stack. If you navigate back (by calling the GoBack method on the NavigationService class) the page will be resumed from its state, without calling the constructor.

However, if you navigate away from an existing page, and re-navigate to the page by calling the Navigate method, a brand new instance of the page is instantiated, and thus the constructor will be called again.

See also: Windows phone 7 - the page life cycle

This means if you are using a navigate method all of your fields are empty because a new instance of the page is created. If you want to keep the state of the TextBlock control you have to store the content temporary. You can use the IsolatedStorageSettings for this purpose, by adding the following using statement:

using System.IO.IsolatedStorage;

You can now save a string (in your case the TextBlock content) to the isolated storage like this:

public void SaveStringObject()
{
    var settings = IsolatedStorageSettings.ApplicationSettings;
    settings.Add("myContent", "foobar");
}

After calling the SaveStringObject method in your navigate method you need to save the settings by calling the IsolatedStorageSettings.Save method.

If you come back to the page containing the TextBlock control you can use this code to retrieve the content of your TextBlock control:

TotalBlock.Text = settings["myContent"].ToString();

The posted code fragments are not complete. You can get a nice sample of using the IsolatedStorage in action right here: IsolatedStorage Sample

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM