Clearing backstack in NavigationService

寵の児 提交于 2019-11-29 09:52:30

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

While I know the original question was for 7, in Windows Phone 8.1 the NavigationService no longer exists.

Here is the Windows Phone 8.1 code

 var previousPage = this.Frame.BackStack.FirstOrDefault();

 if (previousPage != null && previousPage.SourcePageType == typeof(MainPage))
 {
     this.Frame.BackStack.RemoveAt(this.Frame.BackStackDepth - 1);
 }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!