How do I clear navigation history in Silverlight/Windows Phone 7?

前端 未结 5 1444
無奈伤痛
無奈伤痛 2021-02-10 08:18

I\'m making a Windows Phone 7 app that has login/logout semantics (authenticating to a web app). When the user logs out I navigate back to the login screen and forget the sessio

相关标签:
5条回答
  • 2021-02-10 09:05

    There are a few things to look at here. First is I recommend familiarising with the guidance offered here.

    Introducing the concept of “Places” - Peter Torr's Blog

    Redirecting an initial navigation - Peter Torr's Blog (down at the moment sorry)

    If you're able to work within this guidance that is the best path. There is specific advice for dealing with login screens.

    If you are compelled to offer a home button feature. Be careful how you apply this, as certifiers are knocking apps back for behaving in unexpected ways with respect to navigation. I feel if this function is behind a home button you should be ok in the surprise department. How this stands over time with certification we will see. Arguably a "logout" scenario applies equally.

    Here are three approaches from Richard Woo, Maarten Struys, Sam Jarawan.

    0 讨论(0)
  • 2021-02-10 09:05

    I am afraid you cannot clear the stack of pages and what is more, your app will not pass the certification. More info

    0 讨论(0)
  • 2021-02-10 09:20

    This code works well (it needs to be in your page), haven't tried to get it past certification yet but the code does work:

     while (true)
     {
         if (this.RemoveBackEntry() == null)
         {
             break;
         }
      }
    

    This does seem to differ with the documentation, which says an exception will be raised when there is no more items so I have logged a bug on user voice for this, so it may change in future.

    0 讨论(0)
  • 2021-02-10 09:21

    You should create "Loaded" event for your page

        private void LayoutRoot_Loaded(object sender, RoutedEventArgs e)
        {
            try { while (NavigationService.RemoveBackEntry() != null) ; }
            catch (System.NullReferenceException ex) { }
        }
    
    0 讨论(0)
  • 2021-02-10 09:23

    A simpler way to just clear the backstack is to do the following

    while (NavigationService.CanGoBack)
    {
        NavigationService.RemoveBackEntry();
    }
    

    Be aware though that if you do this in the onloaded event it will fire everytime the page is navigated to and clicking back again will close the app!

    Important note, that if you use a webcontrol or the NAX ad system, the backkey is consumed by the page as well as the web control, so will exist the app rather than return to a page.

    **Update Also just found an even shorter hand version from the MS dev center

    while (NavigationService.RemoveBackEntry() != null);
    

    One simple line. Nice

    0 讨论(0)
提交回复
热议问题