【UWP】UWP标题栏的返回按钮

要实现返回到上级按钮有很多方法,为了方便统一,这里采用在标题栏添加返回按钮。
UWP平台提供了一个一致的后退导航系统,用于遍历用户在应用内和应用之间的的导航历史记录。
1.首先在App.xaml.cs类的方法OnLaunched

SystemNavigationManager.GetForCurrentView().BackRequested += BackRequested;

在后面继续添加

SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = rootFrame.CanGoBack ? 
AppViewBackButtonVisibility.Visible : 
AppViewBackButtonVisibility.Collapsed;
rootFrame.Navigated += OnNavigated;

1
2.然后还需要写两个方法来适配

    //非主页面的方法
    private void OnNavigated(object sender, NavigationEventArgs e)
    {
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = 
        ((Frame)sender).CanGoBack ?
            AppViewBackButtonVisibility.Visible : AppViewBackButtonVisibility.Collapsed;
    }
    //返回键的方法
    private void BackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame == null)
            return;
        if (rootFrame.CanGoBack && e.Handled == false)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }

2
下面让我们来看看效果
主页标题栏没有返回按钮
主页
次级页面有返回按钮
次级页面

本文链接:

https://www.veryxs.com/index.php/archives/30/
1 + 1 =
快来做第一个评论的人吧~