Pamp
Pamp

Reputation: 69

How to save content on the page after navigating on it back

I have a page with 4 TextBoxes. Each time pressing on the TextBox I use the NavigationService to navigate to the new page in order to make a comfortable input through Itembox. I use NavigationContext in order to send the selection from the Itembox. But each time navigating back to the first page, constructor of the page gets called and I see empty TextBoxes instead of filled ones.

How can I avoid this?

Upvotes: 0

Views: 254

Answers (1)

Heinrich Ulbricht
Heinrich Ulbricht

Reputation: 10372

Do you actively navigate "back" to your main page using NavigationService.Navigate()? Don't do this, just use NavigationService.GoBack() or let the user use the Back button.

You can transfer the value via PhoneApplicationService.Current.State. Add this to your page where the user enters the value into the Itembox:

protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
{
    PhoneApplicationService.Current.State["name"] = "value";
    base.OnNavigatedFrom(e);
}

This saves the value when the user goes back to the main page. And to your main page code add:

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
    base.OnNavigatedTo(e);

    if (PhoneApplicationService.Current.State.ContainsKey("name"))
        textBox1.Text = (string)PhoneApplicationService.Current.State["name"];
}

This tries to get the value and sets it to the TextBox.

Upvotes: 2

Related Questions