2015-06-02 21 views
20

Làm cách nào tôi xử lý nút quay lại cho cửa sổ dành cho thiết bị di động 10 và nút quay lại cho cửa sổ 10 chế độ máy tính bảng? Tôi đã tìm kiếm ở khắp mọi nơi nhưng không thể tìm thấy bất kỳ ví dụ nào cho nó.Nút quay lại UAP của Windows 10

Trả lời

28

Chủ đề này là một trong các ví dụ được sử dụng trong Guide to Universal Windows Platform apps. Tôi đặc biệt khuyên bạn nên đọc khi bắt đầu với ứng dụng Universal.

Đối với nút trên tiêu đề trang, hãy sử dụng Windows.UI.Core.SystemNavigationManager và đặt thuộc tính AppViewBackButtonVisibility để hiển thị hoặc ẩn nút và xử lý sự kiện BackRequested để thực hiện điều hướng.

Windows.UI.Core.SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; 
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += (s,a) => 
{ 
    Debug.WriteLine("BackRequested"); 
    if (Frame.CanGoBack) 
    { 
     Frame.GoBack(); 
     a.Handled = true; 
    } 
} 

Bạn dây lên các nút phần cứng lại giống như bạn làm trong Windows Phone 8.1, nhưng bạn nên kiểm tra sự PhoneContract (hoặc lớp cá nhân và phương pháp) để đảm bảo nó là ở đó:

if (ApiInformation.IsApiContractPresent ("Windows.Phone.PhoneContract", 1, 0)) { 
    Windows.Phone.UI.Input.HardwareButtons.BackPressed += (s, a) => 
    { 
     Debug.WriteLine("BackPressed"); 
     if (Frame.CanGoBack) 
     { 
      Frame.GoBack(); 
      a.Handled = true; 
     } 
    }; 
} 
+0

AppViewBackButtonVisibility phải được đặt ở đâu? MainPage contstructor không làm bất cứ điều gì cho tôi cũng như OnLaunched trong App.xaml.cs – robertk

+0

Nó tự động bật thanh tiêu đề :) – shady

+1

Có cách nào để tùy chỉnh màu nền của nút quay lại không? –

6

Thêm mã sau đây để App.xaml.cs của bạn và nó sẽ xử lý điều hướng trên máy tính để bàn, máy tính bảng và điện thoại di động (tôi đã thử nghiệm nó trên giả lập điện thoại di động) cho tốt hơn nhấn mạnh sự khác biệt và giải thích (Handling The Back Button In Windows 10 UWP Apps bởi JEFF PROSISE)

sealed partial class App : Application 
{ 

    public App() 
    { 
     this.InitializeComponent(); 
     this.Suspending += OnSuspending; 
    } 

    protected override void OnLaunched(LaunchActivatedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     // Do not repeat app initialization when the Window already has content, 
     // just ensure that the window is active 
     if (rootFrame == null) 
     { 
      // Create a Frame to act as the navigation context and navigate to the first page 
      rootFrame = new Frame(); 

      rootFrame.NavigationFailed += OnNavigationFailed; 
      rootFrame.Navigated += OnNavigated; 

      if (e.PreviousExecutionState == ApplicationExecutionState.Terminated) 
      { 
       // TODO: Load state from previously suspended application 
      } 

      // Place the frame in the current Window 
      Window.Current.Content = rootFrame; 

      // Register a handler for BackRequested events and set the 
      // visibility of the Back button 
      SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested; 

      SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = 
       rootFrame.CanGoBack ? 
       AppViewBackButtonVisibility.Visible : 
       AppViewBackButtonVisibility.Collapsed; 
     } 

     if (rootFrame.Content == null) 
     { 
      // When the navigation stack isn't restored navigate to the first page, 
      // configuring the new page by passing required information as a navigation 
      // parameter 
      rootFrame.Navigate(typeof(MainPage), e.Arguments); 
     } 

     // Ensure the current window is active 
     Window.Current.Activate(); 
    } 

    void OnNavigationFailed(object sender, NavigationFailedEventArgs e) 
    { 
     throw new Exception("Failed to load Page " + e.SourcePageType.FullName); 
    } 

    private void OnNavigated(object sender, NavigationEventArgs e) 
    { 
     // Each time a navigation event occurs, update the Back button's visibility 
     SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = 
      ((Frame)sender).CanGoBack ? 
      AppViewBackButtonVisibility.Visible : 
      AppViewBackButtonVisibility.Collapsed; 
    } 

    private void OnSuspending(object sender, SuspendingEventArgs e) 
    { 
     var deferral = e.SuspendingOperation.GetDeferral(); 
     // TODO: Save application state and stop any background activity 
     deferral.Complete(); 
    } 

    private void OnBackRequested(object sender, BackRequestedEventArgs e) 
    { 
     Frame rootFrame = Window.Current.Content as Frame; 

     if (rootFrame.CanGoBack) 
     { 
      e.Handled = true; 
      rootFrame.GoBack(); 
     } 
    } 
} 
+0

Tôi đang thử tính năng này trên Lumia 950 để ẩn các nút dọc theo nút, quay lại, về nhà và tìm kiếm. Nó không làm bất cứ điều gì và họ vẫn còn đó, bất kỳ ý tưởng tại sao? – Nick

+0

Câu trả lời là liên quan đến nút quay lại đặc biệt xuất hiện ở trên cùng bên trái và xử lý nút phần cứng có hành vi mặc định thoát khỏi ứng dụng. nó không có gì để làm với các nút tìm kiếm nhà và tìm kiếm. –

+0

aah, cảm ơn, xấu của tôi. Tôi tìm thấy thông tin tôi muốn cuối cùng. – Nick

Các vấn đề liên quan