2012-10-03 34 views
8

Tôi có tình huống sau trong ứng dụng tàu điện ngầm WinRT của tôi (C# - xaml):WinRT - Cách bỏ qua hoặc xóa trang khỏi lịch sử điều hướng

Khởi chạy ứng dụng và người đó chưa đăng nhập. Tôi có nút điều hướng họ đến Giỏ hàng. Điều quan trọng cần lưu ý là họ có thể nhấp vào nó bất kể trạng thái đăng nhập/đăng xuất.

Vì vậy, tôi có điều này:

Home Page - > Login Page - > Shopping Cart 

Và mọi thứ hoạt động tuyệt vời, nhưng khi tôi cố gắng bấm nút BACK trên Shopping Cart trang tôi đang lèo lái trở lại Đăng nhập trang, trong đó có ý nghĩa, vì trang nằm trong lịch sử điều hướng của tôi. Nhưng tôi không muốn điều đó, tôi muốn trả lại người dùng cho Trang chủ và bỏ qua trang đăng nhập.

Câu hỏi của tôi là cách thực hiện điều đó và cách thao tác Ngăn xếp điều hướng khung trên WinRT. Tôi đã cố gắng quay trở lại hai lần, nhưng không có may mắn.

Btw, trang của tôi là trang "LayoutAwarePage" và tôi đang sử dụng NavigationService tương tự như http://dotnetbyexample.blogspot.com/2012/06/navigationservice-for-winrt.html này.

Trả lời

12

Bạn có thể tiếp cận theo nhiều cách khác nhau. Bạn có thể làm cho nó để nút quay lại điều hướng trở lại nhiều lần cho đến khi nó đạt đến trang chủ hoặc bỏ qua trang đăng nhập. Bạn cũng có thể làm cho trang đăng nhập xuất hiện bên ngoài điều hướng Frame - trên cửa sổ bật lên hoặc trong một lớp khác trong ứng dụng.

* Cập nhật

Trong 8.1 nền tảng này đã giới thiệu BackStackForwardStack thuộc tính trên Frame mà bạn có thể thao tác.

2

Có tệp LayoutAwarePage.cs trong thư mục Chung của dự án của bạn. Bạn có thể thay đổi hành vi nút quay lại từ tệp này.

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

Bạn có thể gọi GoHome() trên các sự kiện Back nút, mà sẽ đưa bạn đến HomePage hoặc trang đầu tiên của ứng dụng.

+0

Không có 'phương pháp GoHome' trong 'Frame'. Tui bỏ lỡ điều gì vậy? –

+0

@AleksandarToplek Tôi đoán Microsoft đã thay đổi api trong Win 8.1, nó đã tồn tại 2 năm trước khi câu hỏi được hỏi. – Mayank

+0

Ồ, thật lạ lùng. Toàn bộ khung điều hướng trông không đầy đủ đối với tôi. Tôi đã kết thúc triển khai ngăn xếp lịch sử tùy chỉnh. –

-3

Để bật từ stack:

NavigationService.RemoveBackEntry(); 

Để hướng đến Menu chính chạm vào nút quay lại:

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

Để giữ cho người sử dụng trong Menu chính ngay cả khi họ chạm vào nút quay lại:

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

NavigationService không tồn tại trong winrt – swinefeaster

+0

Câu hỏi rõ ràng đề cập đến nó trên Windows Runtime. –

9

Tôi biết nó cũ, nhưng kể từ khi Google tìm thấy trang này cho tôi, có thể ai đó sẽ tìm thấy điều này cũng vậy.

Câu trả lời, trong khi công việc hợp lệ xung quanh, không trả lời câu hỏi.

Bạn có thể sử dụng điều này trên trang đăng nhập, xóa nó khỏi ngăn xếp lại.

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

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

Tôi có may mắn bằng cách sử dụng Frame.BackStack.Remove (Frame.BackStack.Last()) tại trang đích (ví dụ: Giỏ hàng trong trường hợp này). – Hong

-1

Khi tải việc sử dụng trang

this.NavigationCacheMode = NavigationCacheMode.Disabled; 
+0

Không hiệu quả với tôi. Nó vẫn điều hướng đến trang đó. –

0

tôi đã viết dịch vụ điều hướng theo dõi lịch sử của riêng tôi. Bạn có thể tìm thấy nó here.

Trong trường hợp tôi di chuyển tập tin hoặc gỡ bỏ nó, đây là phiên bản hiện tại:

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); 
      } 
     } 
    } 
} 
Các vấn đề liên quan