我创建了这个方法来从页面堆栈中删除多个页面(“任务”)。
/// <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();
}
}
});
}
在第 5 页上,您会像这样使用它:(我认为您正在尝试在注销后返回主菜单。如果没有,请阅读方法签名)
DecreaseBackstackEntryCount(1,() => NavigationService.GoBack());