Come gestisco il pulsante Indietro per Windows Mobile 10 e il pulsante Indietro per Windows 10 modalità tablet? Ho cercato ovunque ma non ho trovato alcun esempio per questo.Windows 10 Pulsante Indietro UAP
risposta
Questo argomento è uno degli esempi utilizzati nello Guide to Universal Windows Platform apps. Consiglio vivamente di leggerlo quando inizi con le app universali.
Per il pulsante nell'intestazione della pagina, utilizzare Windows.UI.Core.SystemNavigationManager e impostare la proprietà AppViewBackButtonVisibility per mostrare o nascondere il pulsante e gestire l'evento BackRequested per eseguire la navigazione.
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;
}
}
eseguire il cablaggio del tasto posteriore dell'hardware lo stesso che si fa in Windows Phone 8.1, ma si dovrebbe verificare per la PhoneContract (o la classe individuale e metodo) per assicurarsi che sia 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;
}
};
}
Aggiungere il seguente codice al App.xaml.cs e sarà gestire la navigazione sul desktop, tablet e mobile (ho provato sull'emulatore mobile) per una migliore evidenziate differenze e spiegazioni (Handling The Back Button In Windows 10 UWP Apps da 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();
}
}
}
Sto provando questo sul mio Lumia 950 per nascondere i pulsanti lungo il pulsante, indietro, casa e ricerca. Non fa nulla e sono ancora lì, qualche idea perché? – Nick
La risposta riguarda lo speciale pulsante indietro che appare in alto a sinistra e gestisce il pulsante hardware che ha un comportamento predefinito di uscire dall'app. non ha nulla a che fare con i pulsanti di casa e di ricerca. –
aah okay grazie, mio male. Alla fine ho trovato le informazioni che volevo. – Nick
Dove deve essere inserito AppViewBackButtonVisibility? MainPage contstructor non fa nulla per me né OnLaunched in App.xaml.cs – robertk
Va automaticamente sulla barra del titolo :) – shady
C'è un modo per personalizzare il colore di sfondo del pulsante Indietro? –