UWP之前,要实现应用内页面后退,Win8.1要自己弄一个后退按钮,WP8.1还可以选择Handle后退按钮事件。
而在UWP中,后退的实现变得更加方便。
在Windows.UI.Core命名空间里,有SystemNavigationManager这么个东西,
只需handle它的BackRequested事件,就可以响应各种系统级的后退事件了,
包括:UWP应用标题栏的后退按钮,Win10平板模式下任务栏上的后退按钮,Win10m的硬件后退按钮,快捷键Win+Backspace, 以及用Cortana触发的后退事件。
1 SystemNavigationManager.GetForCurrentView().BackRequested += 2 App_BackRequested;
private void App_BackRequested(object sender, BackRequestedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if (rootFrame == null) return; // Navigate back if possible, and if the event has not // already been handled . if (rootFrame.CanGoBack && e.Handled == false) { e.Handled = true; rootFrame.GoBack(); } }
另外,标题栏的后退按钮可以通过SystemNavigationManager来控制是否显示。
if (rootFrame.CanGoBack) { // If we have pages in our in-app backstack and have opted in to showing back, do so SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible; } else { // Remove the UI from the title bar if there are no pages in our in-app back stack SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed; }
话说这东西我翻文档翻了半天才找到...
参考:https://msdn.microsoft.com/en-us/library/windows/apps/mt465734.aspx