7

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

Community
  • 1
  • 1
Luke
  • 560
  • 6
  • 26
  • 1
    Trying to manually edit the navigation history is a bad idea. You might just navigate back one and then check if you should continue to navigate backwards. – N_A Dec 26 '12 at 22:38

5 Answers5

5

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

Luke
  • 560
  • 6
  • 26
  • Navigation is fast enough to not notice the animation, but the loadstate event is triggered (and thus logic there will be executed). – Bart Apr 27 '13 at 09:30
  • If you know that you are always going up two levels, I think you could just skip the calls to GoBack() and add the following code in the page you navigate to in its the load state method: "if (Frame.BackStack.Any()) { Frame.BackStack.Clear(); }" – AH. May 11 '15 at 12:45
3
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.

swinefeaster
  • 2,525
  • 3
  • 30
  • 48
2

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);
}
Peter Mols
  • 1,011
  • 1
  • 9
  • 13
0

I have used NavigationCacheMode to avoid multiple instance has been created while Navigate to other page in Frame.

public SamplePage()
    {
        this.InitializeComponent();
        this.NavigationCacheMode = global::Windows.UI.Xaml.Navigation.NavigationCacheMode.Required;
    }
Boobalan
  • 815
  • 11
  • 11
-1

if you are using frame navigation do this.Frame.BackStack.Remove in the loadstate of the new frame and clear out the previous pages a requiued.

gvmani
  • 1,580
  • 1
  • 12
  • 20