1

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??

Viraj Shah
  • 308
  • 1
  • 12

3 Answers3

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