在 UWP 应用程序的 NavigationView 中连接后退按钮

Wiring up back button in NavigationView for UWP app

我在我的 UWP 应用程序中使用 NavigationView 并试图为内容更改连接后退按钮。即使我设置了导航视图的启用 属性,我也无法在内容更改期间启用该按钮。目前我只是希望能够从主视图导航到页面然后返回主视图。并且还能够更深入地导航到堆栈中,以便主视图 -> 新视图 -> 新视图并返回到主视图。

主页。xaml/cs

<NavigationView x:Name="MainView_NavigationView"
                    PaneTitle="Git Manager"
                    PaneDisplayMode="LeftCompact"
                    ItemInvoked="MainView_NavigationView_ItemInvoked"
                    Header="Git Manager"
                    BackRequested="MainView_NavigationView_BackRequested">

    <NavigationView.AutoSuggestBox>
        <AutoSuggestBox PlaceholderText="Search"
                            QueryIcon="Find"/>
    </NavigationView.AutoSuggestBox>

    <NavigationView.MenuItems>
        <NavigationViewItemSeparator/>
        <NavigationViewItemHeader Content="Information" />
        <NavigationViewItemSeparator/>

        <NavigationViewItem x:Name="Login_NavViewItem" 
                                Content="Login" 
                                Tag="Clone Repositories" 
                                Icon="AddFriend"/>

        <NavigationViewItem x:Name="Remote_NavViewItem" 
                                Content="Remote" 
                                Icon="World"/>

        <NavigationViewItem x:Name="Local_NavViewItem" 
                                Content="Local" 
                                Icon="Folder"/>

        <NavigationViewItem x:Name="Schedule_NavViewItem" 
                                Content="Schedule" 
                                Icon="Calendar"/>

        <NavigationViewItemSeparator/>
    </NavigationView.MenuItems>

    <NavigationView.Content>
        <Frame x:Name="ContentFrame">
            <Frame.ContentTransitions>
                <TransitionCollection>
                    <NavigationThemeTransition />
                </TransitionCollection>
            </Frame.ContentTransitions>
            <Grid>
                <TextBlock HorizontalAlignment="Center" VerticalAlignment="Center" HorizontalTextAlignment="Center" TextAlignment="Center" Text="asdfasdfasdfasdf" />
            </Grid>
        </Frame>
    </NavigationView.Content>
</NavigationView>

-

public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            InitializeComponent();
        }

        private void MainView_NavigationView_ItemInvoked(NavigationView sender, NavigationViewItemInvokedEventArgs args)
        {
            if(args.IsSettingsInvoked)
            {

            }
            else
            {
                NavigationViewItem item = args.InvokedItemContainer as NavigationViewItem;

                switch(item.Content)
                {
                    case "Login":
                        ContentFrame.Navigate(typeof(Views.Login));
                        break;
                    case "Remote":
                        ContentFrame.Navigate(typeof(Views.Remote));
                        break;

                    case "Local":
                        ContentFrame.Navigate(typeof(Views.Local));
                        break;

                    case "Schedule":
                        ContentFrame.Navigate(typeof(Views.Schedule));
                        break;
                }
            }
        }

        private async void MainView_NavigationView_BackRequested(NavigationView sender, NavigationViewBackRequestedEventArgs args)
        {
            var contentBox = new ContentDialog();
            contentBox.Content = "Back clicked";

            await contentBox.ShowAsync();
        }
    }

其他页面只是上面有文本块的新内容页面。所以我的 2 个问题是:

如何连接后退按钮以实际启用并在我更改内容时能够单击它,或者如何导航到导航视图内的新页面以启用此 button/functionlity。

如何使用它来回导航嵌套页面?

How do I wire up the back button to actually be enabled and able to click it when I change my content or how I would navigate to a new page inside the navigation view to enable this button/functionlity.

要enable/disbale NavigationView 中的BackButton,您需要为其设置IsBackEnabled=True/False 属性。然后,您可以使用 Frame class CanGoBackCanGoForward 属性 制作后退按钮 enable/disable.

请参阅 Navigation View and Navigation history and backwards navigation for UWP apps 了解更多信息。