1

I have app which contain 5 page, end last page will be called signout so once I clicked to the signout it will remove all the pagestack and start to navigate from the page 1.

eg: page-1 MainMenu

page-2 list of Items.

page-3 which Common Sharing Options(of Facebook and twiter)

page-4 Point showing list which is highest earned point.

page-5 sign-out.

Once I clicked on Sign-out it will goes to Login Page. and Once it logged In it will redirect to me page -5.

so my question is I want to redirect to the page 2. so how I can do this.

I had tried

        foreach (var journalEntry in ((PhoneApplicationFrame)Application.Current.RootVisual).BackStack)
        {
            ((PhoneApplicationFrame)Application.Current.RootVisual).RemoveBackEntry();
        }

but it will not resolved my problem.

Thank you..!!

Jitendra Jadav
  • 93
  • 3
  • 12

4 Answers4

1

You can remove entries from the BackStack using the NavigationService.RemoveBackEntry method

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166
  • Thank you @Rowland Shaw I am using this code but still problem is not resolved. foreach (var journalEntry in ((PhoneApplicationFrame)Application.Current.RootVisual).BackStack) { ((PhoneApplicationFrame)Application.Current.RootVisual).RemoveBackEntry(); break; } – Jitendra Jadav May 22 '12 at 12:24
  • You don't want to remove all the backstack entries - just the ones that relate to the "logged in" states – Rowland Shaw May 22 '12 at 12:27
  • I am sorry but I had added into foreach loop that time it will give me error so I have manually added 4 times foeareach and breack it for the time being. – Jitendra Jadav May 22 '12 at 12:42
1

You may be interested in reading the following "Solving Circular Navigation in Windows Phone Silverlight Applications" :-

http://windowsteamblog.com/windows_phone/b/wpdev/archive/2010/12/13/solving-circular-navigation-in-windows-phone-silverlight-applications.aspx

Paul Diston
  • 3,279
  • 1
  • 17
  • 20
1

I created this method for removing multiple pages ('tasks') from the page stack.

/// <summary>
        /// Decreases the back stack entry count, leaving the amount of items on the stack equilivent to the <param name="leaveAmount">leaveAmount</param>.
        /// </summary>
        /// <exception cref="NotSupportedException">A value less than 0 is provided</exception>
        /// <param name="leaveAmount">The leave amount.</param>
        /// <param name="whenFinished"> </param>
        public static void DecreaseBackStackEntryCount(int leaveAmount, Action whenFinished = null)
        {
            if (leaveAmount < 0)
            {
                throw new NotSupportedException("cannot remove every item on stack");
            }
            Deployment.Current.Dispatcher.BeginInvoke(() =>
                                                          {
                                                              try
                                                              {
                                                                  while (((PhoneApplicationFrame)Application.Current.RootVisual).BackStack.Count() > leaveAmount)
                                                                  {
                                                                      try
                                                                      {
                                                                          ((PhoneApplicationFrame) Application.Current.RootVisual).RemoveBackEntry();
                                                                      }
                                                                      catch (InvalidOperationException)
                                                                      {
                                                                          return;
                                                                      }
                                                                  }

                                                              }
                                                              catch
                                                              {
                                                              }
                                                              finally
                                                              {
                                                                  if (whenFinished != null)
                                                                  {
                                                                        whenFinished.Invoke();
                                                                  }
                                                              }
                                                          });
        }

on page 5, you'd use it like: (I THINK you're trying to go back to the main menu after signout.. if not, read the method signature)

DecreaseBackstackEntryCount(1,() => NavigationService.GoBack());
William Melani
  • 4,270
  • 1
  • 21
  • 44
1

Part of your issue may be that you are modifying the collection in your foreach loop and that's not going to work.

Here is a SO question that gives you some options for that in general situation. The gist is to use a for loop going backwards or to call .ToArray() or .ToList() and do your foreach on those separate collections.

However what I would do in this situation would be:

while (NavigationService.CanGoBack)
{
  NavigationService.RemoveBackEntry();
}
Community
  • 1
  • 1
Austin Thompson
  • 2,251
  • 1
  • 17
  • 23