0

I'm developing a WP 8 app and I've a problem with navigation.

I can't find a way to navigate to a page on my back stack without losing my actual page state.

The situation is this:

  1. I'm on page A and navigate to page B, so the back stack only contains A.
  2. On page B I want to navigate back to page A but with page B being persisted on the back stack, so when I'm on page A again I can go back to page B preserving it state.

So this is it: A --> B --> A --> B, it's kind of a cycle but it isn't one. Page A shows items that are also shown in page B with less detail, but from page B I can go to page A to see the details and then back and back again.

It may seem complicated but I assure you its pretty intuitive :) it works like this on Android and iOS.

Is there anyway to add page B to the navigation stack before page A so when I call NavigationService.GoBack() it goes back to page A with it previous state and then do the same from A to B.

Thanks in advance!!!

Martin
  • 660
  • 10
  • 23

2 Answers2

1

There are a couple of options. If you use the standard MVVM pattern then you're not really saving "page state" (unless you mean things like scroll position in lists or selected text control) - you're saving data. And that data will be there the next time you navigate to Page B. You can watch this video and the MSDN article it points to for more information on databinding and how to store the data in global state that will be re-bound to the page the next time you navigate.

If databinding doesn't solve the problem, another option is to not use different pages at all, but to just have A and B as two different containers on the same physical page. You can then trap the back button to fake going "back" when you need to toggle back to Container A.

Peter Torr - MSFT
  • 11,824
  • 3
  • 18
  • 51
  • Hi! Thanks for the reply. The first option won't work because on WP you either discard the page to go to a previous one or create a new page to go forward when navigating. The only way to reload a previous page is by going back, and the only way to save the state of a page is going forward. I need to go back and save the state and that isn't posible. As for the second solution, both pages add up some complexity and it isn't posible to solve it this way. At first it was done like that but then it had to be divided into two pages for performance issues. – Martin Feb 09 '15 at 12:44
  • Either the UI is simple enough to hold both "pages" in memory at once (the second solution, which you say is not possible) or else you need to unload the UI (which is what the first option does). Did you follow the links to the video / article? Databinding is not affected by the fact that the UI objects are destroyed. – Peter Torr - MSFT Feb 09 '15 at 16:10
0

Use this help topic to learn how to preserve and restore page state for Windows Phone 8. Navigation stack can store only page URI and a small amount of data (in the URI parameter).

Like this:

NavigationService.Navigate(new Uri("/SecondPage.xaml?var=value", UriKind.Relative));

At the SecondPage.xaml you can get the value of val. Here is detailed example.

 if (NavigationContext.QueryString.TryGetValue("val", out msg))
crea7or
  • 4,421
  • 2
  • 26
  • 37