2015-06-02 28 views

Odpowiedz

28

Ten temat jest jednym z przykładów używanych w Guide to Universal Windows Platform apps. Sugeruję przeczytanie tej wersji, gdy zaczniesz korzystać z aplikacji Universal.

Dla przycisku w nagłówku strony użyj Windows.UI.Core.SystemNavigationManager i ustaw właściwość AppViewBackButtonVisibility, aby pokazać lub ukryć przycisk i obsłużyć zdarzenie BackRequested w celu wykonania nawigacji.

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

można drut aż przycisk sprzętowy z powrotem tak samo, jak to zrobić w Windows Phone 8.1, ale należy sprawdzić, czy PhoneContract (lub poszczególne klasy i metody), aby upewnić się, że jest tam:

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

Gdzie należy umieścić AppViewBackButtonVisibility? Konturator MainPage nic nie robi dla mnie ani OnLaunched w App.xaml.cs – robertk

+0

Automatycznie przechodzi na pasek tytułu :) – shady

+1

Czy istnieje sposób na dostosowanie koloru tła przycisku Wstecz? –

6

Dodaj poniższy kod do App.xaml.cs i będzie obsługiwać nawigację na komputerach, tabletach i urządzeniach mobilnych (testowałem go na telefon emulatora) dla lepiej podkreślone różnice i wyjaśnienia (Handling The Back Button In Windows 10 UWP Apps przez 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

Próbuję tego na mojej Lumii 950, aby ukryć przyciski wzdłuż przycisku, z powrotem, domu i wyszukiwania. Nic nie robi i nadal tam są, wszelkie pomysły, dlaczego? – Nick

+0

Odpowiedź dotyczy specjalnego przycisku Wstecz, który pojawia się w lewym górnym rogu i obsługi przycisku sprzętowego, który ma domyślne zachowanie wychodzenia z aplikacji. nie ma to nic wspólnego z przyciskami home i wyszukiwania. –

+0

aah ok, dzięki, moja zła. Znalazłem informacje, które chciałem w końcu. – Nick

Powiązane problemy