Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clearing backstack in NavigationService

I am navigating through different pages within my application. After I login, I come to home page from where the navigation starts. During navigation, when I come to homepage, I want to go to the login page by pressing BackKey but I can only navigate to previously navigated page. I could have overriden the BackKeyPress event to navigate to the Login Page but in LoginPage I should again override the Backkeypress otherwise there appears to be cycle between loginpage and homepage on backkey press. Is there anyway to clear the navigation history?

like image 239
master.fake Avatar asked Nov 23 '11 11:11

master.fake


1 Answers

You can use NavigationService.RemoveBackEntry: http://msdn.microsoft.com/en-us/library/system.windows.navigation.navigationservice.removebackentry%28v=VS.92%29.aspx

For instance, to remove all entries from the stack:

while (this.NavigationService.BackStack.Any())
{
   this.NavigationService.RemoveBackEntry();
}


Also, if you want to remove only the previous page after checking its URI:
var previousPage = this.NavigationService.BackStack.FirstOrDefault();

if (previousPage != null && previousPage.Source.ToString().StartsWith("/MainPage.xaml"))
{
    this.NavigationService.RemoveBackEntry();
}
like image 73
Kevin Gosse Avatar answered Oct 31 '22 11:10

Kevin Gosse