Reputation: 453
I have to develop an WP8 app that uses a cyclic navigation. I'm from Android and I'm new to WP8, so forgive me if I say something stupid about it.
When the users start my app, he starts at page A.xaml then he can navigate to C.xaml with a left button or B.xaml with the right button. From B.xaml he can go back to A.xaml with left and to C.xaml with right, while from C.xaml the right leads back to A.xaml while the left leads to B.xaml. I have 3 integers: NA, NB and NC than can respectively be modified only in A, B or C, but are displayed in all 3, so I always pass these parameters.
Likely the user will navigate between "pages" pretty fast, so I wouldn't mind keep them active instead of destroying and recreating them every time.
My initial code was something like this (I omitted the part where I display NA, NB and NC):
A.xaml:
int NA = 0;
int NB = 0;
int NC = 0;
public APage()
{
InitializeComponent();
NA= Convert.ToInt32(PhoneApplicationService.Current.State["NA"]);
NB= Convert.ToInt32(PhoneApplicationService.Current.State["NB"]);
NC= Convert.ToInt32(PhoneApplicationService.Current.State["NC"]);
UpdateA(NA);
UpdateB(NB);
UpdateC(NC);
}
public void ToC_click(object sender, RoutedEventArgs e)
{
PhoneApplicationService.Current.State["NA"] = NA;
PhoneApplicationService.Current.State["NB"] = NB;
PhoneApplicationService.Current.State["NC"] = NC;
NavigationService.Navigate(new Uri("/C.xaml", UriKind.Relative));
}
public void ToB_click(object sender, RoutedEventArgs e)
{
PhoneApplicationService.Current.State["NA"] = NA;
PhoneApplicationService.Current.State["NB"] = NB;
PhoneApplicationService.Current.State["NC"] = NC;
NavigationService.Navigate(new Uri("/B.xaml", UriKind.Relative));
}
B.xaml has ToA_click
and ToC_click
methods, while C.xaml has ToA_click
and ToB_click
, wich vary only for the uri in the navigation.
Doing so, when I navigate A->B->C or A->C->B I have no problem, but when try A->B->C->A or A->C->B->A my app crashes.
I thought I was keep stacking into something like ABCA and getting an error, so I modified the "back" button to NavigationService.GoBack()
if I navigate from B to A or from C to B. With that modification I am able to A->B->C than backward or A->C->B, but still I cannot A->B->C->A or A->C->B->A.
I suppose that at this point I have all 3 xaml stacked and so if I can't go back I have to clear the stack and then rebuild it, but doing so I would have to handle the reset by myself (with some tricky variable) in A.xaml and C.xaml.
Is that the correct way to implement it? I thought that having some kind of flag that avoid recreating the xaml would have been convenient instead of "playing" with the stack, but I haven't found anything about it. I also feel like I am missing something, did I misunderstood WP8 stacking?
Upvotes: 0
Views: 74
Reputation: 12019
In addition to using a Panorama
or Pivot
control (as suggested by @Bas) I would consider looking at this not as something to be solved with Page
s but by having a single Page
that shows or hides different UserControl
s based on the buttons being pressed.
Basically use the UserControl template in Project -> Add New Item instead of a Page and then instead of using NavigationService.Navigate
to go between the three states, you create the three controls inside MainPage.xaml
and toggle the Visibility
of each one to show it at the right time.
Upvotes: 1