0

I am trying to remove the specific BackStack entry from NavigationService.BackStack.

I found there is one method to remove back entry i.e. NavigationService.RemoveBackEntry();, but it remove all back entries from stack. So, my question is How to remove specific back entry?

How can I remove the query string from NavigationService.CurrentSource? I want retrieve only Uri without Query String.

Thank you in advance :)

Ajay
  • 6,418
  • 18
  • 79
  • 130
  • 1
    You can't do that and you shouldn't because it would mess with people's expectation of what happens when you click the Back button. – Toni Petrina Sep 05 '13 at 13:04

2 Answers2

3

You can't remove specific back stack entity, because your current stack(current page) can interact with its adjacent stack only, So can remove only its previous stack entity. You need to remove stack at time of NavigateTo() next page.

Abhishek
  • 995
  • 13
  • 28
-1

I can do that with this code. Insert this into a page.Loaded event:

int backstackcounter = this.NavigationService.BackStack.Count();

            for (int i = 0; i < backstackcounter; i++)
            {
               var previousPage = this.NavigationService.BackStack.FirstOrDefault();

                if (previousPage != null && !previousPage.Source.ToString().StartsWith("/LoginPage.xaml"))
                {
                    this.NavigationService.RemoveBackEntry();
                }
            }

The "LoginPage.xaml" is a page that i want to stay in the backstack.

Freeubi
  • 336
  • 2
  • 11
  • You cant traverse the BackStack this way. this.NavigationService.BackStack.FirstOrDefault() will always give you the first element. plus RemoveBackEntry() will always remove the most recent element. SO if you most recent element is no the login page, this code will never work. – Muhammad Mar 20 '14 at 08:10