简体   繁体   English

如何在导航和返回页面时保留文本字段数据

[英]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). 导航到该页面时会实例化页面(假设您在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. 如果您向后导航(通过调用NavigationService类上的GoBack方法),页面将从其状态恢复,而不调用构造函数。

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. 但是,如果您离开现有页面并通过调用Navigate方法重新导航到该页面,则会实例化一个全新的页面实例,因此将再次调用构造函数。

See also: Windows phone 7 - the page life cycle 另请参阅: Windows phone 7 - 页面生命周期

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. 如果要保持TextBlock控件的状态,则必须临时存储内容。 You can use the IsolatedStorageSettings for this purpose, by adding the following using statement: 您可以通过添加以下using语句将IsolatedStorageSettings用于此目的:

using System.IO.IsolatedStorage;

You can now save a string (in your case the TextBlock content) to the isolated storage like this: 您现在可以将字符串(在您的情况下为TextBlock内容)保存到隔离存储中,如下所示:

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. 在导航方法中调用SaveStringObject方法后,您需要通过调用IsolatedStorageSettings.Save方法来保存设置。

If you come back to the page containing the TextBlock control you can use this code to retrieve the content of your TextBlock control: 如果您回到包含TextBlock控件的页面,则可以使用此代码检索TextBlock控件的内容:

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 您可以在此处获得使用IsolatedStorage的好示例: IsolatedStorage示例

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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