How do I save values between page navigation in windows phone, suppose I have two text blocks in my one phone application page, and they contains dynamically changing values every time, now suppose my text block have value "abc" and for some reason I go back to previous page, now when I get back on my page, I want that text block having value "abc". How to do it??
Asked
Active
Viewed 670 times
1
-
assign textblock value in a globally defined static variable. – Jaihind Apr 01 '14 at 06:33
-
@Jaihind how to do it..show me by code brother..thanks – Viraj Shah Apr 01 '14 at 06:37
-
I give a ans. Give a look and let me know if works or not. – Jaihind Apr 01 '14 at 06:46
-
There are various methods to save state of your variables. You can adapt most of the answers from [your other question](http://stackoverflow.com/q/22705291/2681948) for this purpose. – Romasz Apr 01 '14 at 07:52
-
@Jaihind I am checking it..working on it..thanks!! – Viraj Shah Apr 01 '14 at 08:40
3 Answers
3
There are several methods available
IsolatedStorageSettings
Save
IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
// txtInput is a TextBox defined in XAML.
if (!settings.Contains("userData"))
{
settings.Add("userData", txtInput.Text);
}
else
{
settings["userData"] = txtInput.Text;
}
settings.Save();
Read
if (IsolatedStorageSettings.ApplicationSettings.Contains("userData"))
{
txtDisplay.Text +=
IsolatedStorageSettings.ApplicationSettings["userData"] as string;
}
PhoneApplicationService.Current.State
PhoneApplicationService.Current.State["param"] = param
and on other page we can get it like this.
var k = PhoneApplicationService.Current.State["param"];

Alen Lee
- 2,479
- 2
- 21
- 30
2
Define two static variable in your App.xaml.cs
public static valueOne = string.Empty;
public static valueTwo = string.empty;
//Assign textbox value to variable on page leaving event
protected override void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e)
{
if(!string.IsNullOrEmpty(txtBoxOne.Text))
App.valueOne = txtBoxOne.Text;
if(!string.IsNullOrEmpty(txtBoxTwo.Text))
App.valueTwo = txtBoxTwo.text;
}
//Get value from page load
protected override void OnNavigatedTo(NavigationEventArgs e)
{
if(!string.IsNullOrEmpty(App.valueOne))
string valueFirst = App.valueOne;
if(!string.IsNullOrEmpty(App.valueTwo ))
string valueTwo = App.valueTwo ;
}

Jaihind
- 2,770
- 1
- 12
- 19
1
There are various approaches to solve this. Common thing is using a Static Class, which holds static properties and binding it to your View.

Arun Selva Kumar
- 2,593
- 3
- 19
- 30