2015-06-02 110 views
20

我將如何處理windows mobile 10的後退按鈕和windows 10 tablet模式的後退按鈕?我一直在尋找,但找不到任何示例。Windows 10 UAP後退按鈕

回答

28

此主題是Guide to Universal Windows Platform apps中使用的示例之一。我強烈建議您在開始使用Universal應用程序時閱讀這些內容。

對於頁眉上的按鈕,使用Windows.UI.Core.SystemNavigationManager並設置AppViewBackButtonVisibility屬性以顯示或隱藏按鈕並處理BackRequested事件以執行導航。

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

連線了硬件後退按鈕一樣的,你在的Windows Phone 8.1做的,但您應檢查P​​honeContract(或個人類和方法),以確保它的存在:

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應該放在哪裏? MainPage contstructor不會爲我做任何事情,也不會在App.xaml.cs中執行OnLaunched – robertk

+0

它會自動進入標題欄:) – shady

+1

有沒有什麼方法可以自定義後退按鈕的背景顏色? –

6

下面的代碼添加到您的App.xaml.cs,它會處理在桌面,平板電腦和移動導航(我測試了它在移動仿真器) 更好地突出差異和(由JEFF PROSISEHandling The Back Button In Windows 10 UWP Apps)解釋

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

我試圖在我的Lumia 950上隱藏按鈕,按鈕,回家,搜索按鈕。它什麼都不做,他們仍然存在,有什麼想法爲什麼? – Nick

+0

答案是關於左上角出現的特殊返回按鈕,並處理具有退出應用的默認行爲的硬件按鈕。它與家庭和搜索按鈕沒有任何關係。 –

+0

啊好的謝謝,我的壞。我最終找到了我想要的信息。 – Nick