Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pop pages off navigation stack in Windows 8 App

In navigating between pages I see that there are functions letting you go back / forward or to the 'home' page. However, what I need is to be able to go back but skip over pages that the user shouldn't be able to visit any more. I searched around and found the following: http://social.msdn.microsoft.com/Forums/en-US/winappswithcsharp/thread/3819f389-3bfc-4c59-a919-272927fc9229

I tried using GetNavigationStack, but it keeps failing due to not being able to serialize and object I pass as the NavigationParameter. I'm having to use a Tuple to pass 2 parameters, but this can not be serialized and so I can not get the Navigation Stack to edit it and remove the pages manually.

I then found this WinRT - How to ignore or delete page from navigation history and thought I could include a boolean value that could be set if the page was to be skipped over

In LayoutAwarePage.cs (the class all screens inherit from, generated by Visual Studio) I added protected bool CanGoBackToThisPage { get; set; } which can be set if you don't want to visit this page, but there is also a problem with this as some times I wont know if you want to be able to visit a page again until you have done something on the current page. I don't think you can do something like previousFrame.CanGoBackToThisPage = true. Also, the Frame object you have access to doesn't (I think) have access to the actual page objects, but some summary object instead and I don't know how I'd go about getting this boolean into that summary object.

Does anyone know of a simple way to remove the previous / current page from the navigation stack? This seems like it would be a really common problem so I struggle to think Microsoft hasn't given us some way of doing it.

Thanks

like image 558
Luke Avatar asked Dec 26 '12 22:12

Luke


3 Answers

Ok, I don't know why I didn't think of it before posting the question. Maybe I'm just tired.

I just called Frame.GoBack() twice and then navigated to the page I want to go to:

// If the user presses back we don't want them to visit this page or the previous page they were on so remove them from the navigation stack
Frame.GoBack();
Frame.GoBack();

// Go to the detail page
this.Frame.Navigate(typeof(GroupDetailPage), mGroup);

I guess I thought calling GoBack() would actually make the page animate to the previous one, but it doesn't

like image 146
Luke Avatar answered Nov 16 '22 00:11

Luke


Frame.BackStack.RemoveAt(Frame.BackStack.Count - 1);

Though I have to give props to gvmani because his answer helped, though it wasn't very complete.

This will avoid the problems mentioned above with the loadstate event being triggered and is a more elegant solution.

like image 31
swinefeaster Avatar answered Nov 16 '22 01:11

swinefeaster


I ran into the same issue and have solved this by removing current page from the BackStack after navigate away:

protected override void OnNavigatedFrom(NavigationEventArgs e)
{
  base.OnNavigatedFrom(e);

  // Remove current page from history
  var pageStackEntry = Frame.BackStack.Last(entry => entry.SourcePageType == this.GetType());
  Frame.BackStack.Remove(pageStackEntry);
}
like image 42
Peter Mols Avatar answered Nov 16 '22 00:11

Peter Mols