2012-10-03 51 views
8

我在WinRT中的地铁下面的情况导航历史(C# - XAML)删除页面应用:WinRT的 - 如何忽略或

用户推出,并没有登录,他或她的应用程序在菜单栏我有按钮,导航到购物车。重要的是要提到,无论登录/注销状态如何,他们都可以点击它。

所以我有这样的:

Home Page - > Login Page - > Shopping Cart 

而且一切都很正常,但是当我尝试我购物车页面按BACK按钮我导航回到登录页,这是有意义的,因为页面在我的导航历史记录中。但我不想那样,我想将用户返回到主页并跳过登录页面。

我的问题是如何做到这一点,以及如何操作WinRT上的帧导航堆栈。我试着回去两次,但没有运气。

顺便说一句,我的页面是“LayoutAwarePage”页面,我正在使用NavigationService类似于此 http://dotnetbyexample.blogspot.com/2012/06/navigationservice-for-winrt.html

回答

12

你可以用不同的方法来处理它。您可以这样做,以便后退按钮多次导航,直到它到达主页或跳过登录页面。您还可以使页面登录页面显示在导航Frame以外的地方 - 无论是在弹出窗口还是在应用程序的不同层。

*更新

在8.1的平台上引入Frame,您可以操控的BackStackForwardStack性能。

2

项目的Common文件夹中有一个LayoutAwarePage.cs文件。 您可以从此文件更改后退按钮行为。

protected virtual void GoBack(object sender, RoutedEventArgs e) 
     { 

      while (this.Frame.CanGoBack) this.Frame.GoBack(); 

      // Use the navigation frame to return to the previous page 
      //if (this.Frame != null && this.Frame.CanGoBack) this.Frame.GoBack(); 
     } 
0

你可以叫上Back按钮事件GoHome(),是带你去HomePage或应用程序的第一页。

+0

有一个在'Frame'没有'GoHome'方法。我错过了什么吗? –

+0

@AleksandarToplek我猜微软已经改变了Win 8.1的api,它在2年前当问题被问到时确实存在。 – Mayank

+0

哦,这很奇怪。整个导航框架对我来说看起来不完整。我结束了实现自定义历史堆栈。 –

-3

从栈中弹出:

NavigationService.RemoveBackEntry(); 

导航到主菜单触摸后退按钮:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
{ 
    NavigationService. Navigate (new Uri ("/Main Page. xaml", UriKind.Relative)); 
} 

为了使用户在即使他们接触后退按钮主菜单:

protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e) 
{ 
    // cancel the navigation 
    e.Cancel = true; 
} 
+0

NavigationService在winrt中不存在 – swinefeaster

+0

这个问题清楚地提到它在Windows运行时。 –

9

我知道它已经很旧了,但自从Google为我找到了这个页面,也许别人会发现这个页面。

答案虽然是有效的解决方法,但并未回答问题。

您可以在登录页面上使用它,将其从背面堆栈中删除。

if(login_was_successful == true) 
{ 
    this.Frame.Navigate(typeof(ShoppingCard)); 

    if(this.Frame.CanGoBack) 
    { 
     this.Frame.BackStack.RemoveAt(0); 
    } 
} 
+1

我很幸运,在目标页面使用Frame.BackStack.Remove(Frame.BackStack.Last())(例如在这种情况下的购物车)。 – Hong

-1

当加载页面使用

this.NavigationCacheMode = NavigationCacheMode.Disabled; 
+0

不适用于我。它仍然导航到该页面。 –

0

我写我自己的历史跟踪导航服务。你可以找到它here

如果我移动文件或删除它,这里的当前版本:

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading; 
using Windows.UI.Xaml; 
using Windows.UI.Xaml.Controls; 
using Windows.UI.Xaml.Media.Animation; 
using Windows.UI.Xaml.Navigation; 
using MetroLog; 
using SparkiyClient.UILogic.Services; 

namespace SparkiyClient.Services 
{ 
    public class NavigationService : INavigationService 
    { 
     private static readonly ILogger Log = LogManagerFactory.DefaultLogManager.GetLogger<NavigationService>(); 
     private readonly Dictionary<string, Type> pagesByKey = new Dictionary<string, Type>(); 
     private readonly Stack<PageStackEntry> historyStack = new Stack<PageStackEntry>(); 
     private PageStackEntry currentPage; 

     public string CurrentPageKey { get; private set; } 

     public bool CanGoBack => this.historyStack.Any(); 

     private static Frame GetFrame() 
     { 
      return (Frame)Window.Current.Content; 
     } 

     public void GoBack() 
     { 
      if (!this.CanGoBack) 
       return; 

      var item = this.historyStack.Pop(); 
      this.NavigateTo(item.SourcePageType.Name, item.Parameter, false); 
     } 

     public void GoHome() 
     { 
      if (!this.CanGoBack) 
       return; 

      var item = this.historyStack.Last(); 
      this.NavigateTo(item.SourcePageType.Name, item.Parameter, false); 
      this.historyStack.Clear(); 
     } 

     public void NavigateTo(string pageKey, bool addSelfToStack = true) 
     { 
      this.NavigateTo(pageKey, null, addSelfToStack); 
     } 

     public void NavigateTo<T>(bool addToStack = true) 
     { 
      this.NavigateTo<T>(null, addToStack); 
     } 

     public void NavigateTo<T>(object parameter, bool addSelfToStack = true) 
     { 
      this.NavigateTo(typeof(T).Name, parameter, addSelfToStack); 
     } 

     public void NavigateTo(string pageKey, object parameter, bool addToStack = true) 
     { 
      var lockTaken = false; 
      Dictionary<string, Type> dictionary = null; 
      try 
      { 
       Monitor.Enter(dictionary = this.pagesByKey, ref lockTaken); 
       if (!this.pagesByKey.ContainsKey(pageKey)) 
        throw new ArgumentException(string.Format("No such page: {0}. Did you forget to call NavigationService.Configure?", pageKey), "pageKey"); 

       if (addToStack && this.currentPage != null) 
        this.historyStack.Push(this.currentPage); 

       GetFrame().Navigate(this.pagesByKey[pageKey], parameter); 

       this.CurrentPageKey = pageKey; 
       this.currentPage = new PageStackEntry(this.pagesByKey[pageKey], parameter, null); 

       Log.Debug(this.historyStack.Reverse().Aggregate("null", (s, entry) => s + " > " + entry.SourcePageType.Name)); 
      } 
      finally 
      { 
       if (lockTaken && dictionary != null) 
        Monitor.Exit(dictionary); 
      } 
     } 

     public void Configure(string key, Type pageType) 
     { 
      var lockTaken = false; 
      Dictionary<string, Type> dictionary = null; 
      try 
      { 
       Monitor.Enter(dictionary = this.pagesByKey, ref lockTaken); 
       if (this.pagesByKey.ContainsKey(key)) 
        this.pagesByKey[key] = pageType; 
       else this.pagesByKey.Add(key, pageType); 
      } 
      finally 
      { 
       if (lockTaken && dictionary != null) 
        Monitor.Exit(dictionary); 
      } 
     } 
    } 
}