Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
919 views
in Technique[技术] by (71.8m points)

windows phone 7 - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...