简体   繁体   English

UWP导航窗格返回按钮不起作用

[英]UWP Navigation Pane go back button not working

I'm learning UWP and trying to implement GO BACK button in a navigation pane. 我正在学习UWP并试图在导航窗格中实现GO BACK按钮。 I put go-back button under a RelativePanel right below menu button. 我在菜单按钮下面的RelativePanel下面放了一个go-back按钮。 The below is my current XAML page: 以下是我目前的XAML页面:

<Page
    x:Class="LearningUWP.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:LearningUWP"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:muxc="using:Windows.UI.Xaml.Controls"
    mc:Ignorable="d"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" >
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <RelativePanel>
            <Button Name="Menu" FontFamily="Segoe MDL2 Assets" Content="&#xE700;" FontSize="36" Click="Menu_Click"></Button>
            <Button RelativePanel.Below="Menu" Style="{StaticResource NavigationBackButtonNormalStyle}" Name="Back" FontSize="36" Click="Back_Click"></Button> 
        </RelativePanel>
        <SplitView Name="MySplitView"
                   Grid.Row="1"
                   DisplayMode="CompactOverlay"
                   OpenPaneLength="200"
                   CompactPaneLength="56"
                   HorizontalAlignment="Left">
            <SplitView.Pane>
                <ListBox SelectionMode="Single"
                         Name="IconsListBox"
                         SelectionChanged="IconsListBox_SelectionChanged"
                         >
                    <ListBoxItem Name="ShareListBoxItem">
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE72D;"/>
                            <TextBlock Text="Share" FontSize="24" Margin="20, 0, 0, 0"/>
                        </StackPanel>
                    </ListBoxItem>
                    <ListBoxItem Name="FavoritesListBoxItem" >
                        <StackPanel Orientation="Horizontal" >
                            <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="36" Text="&#xE734;"/>
                            <TextBlock Text="Favorites" FontSize="24" Margin="20, 0, 0, 0"/>
                        </StackPanel>
                    </ListBoxItem>
                </ListBox>
            </SplitView.Pane>
            <SplitView.Content>
                <TextBlock Margin="50, 0, 0, 0" Name="ResultTextBlock"/>
            </SplitView.Content>
        </SplitView>
    </Grid>
</Page>

And the XAML's code-behind: 而XAML的代码隐藏:

namespace LearningUWP
{
    public sealed partial class MainPage : Page
    {
        public MainPage()
        {
            this.InitializeComponent();
        }
        private void Menu_Click(object sender, RoutedEventArgs e)
        {
            MySplitView.IsPaneOpen = !MySplitView.IsPaneOpen;
        }
        private void IconsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ShareListBoxItem.IsSelected)
                ResultTextBlock.Text = "shared";
            else if (FavoritesListBoxItem.IsSelected)
                ResultTextBlock.Text = "Favorites";
        }
        private void Back_Click(object sender, RoutedEventArgs e)
        {
            if (this.Frame.CanGoBack)
                this.Frame.GoBack();
        }}}

For some reason, after I click the Go back button, it doesn't work as expected, and what is more, I find this.Frame.CanGoBack = false . 出于某种原因,在我单击“返回”按钮后,它无法按预期工作,更重要的是,我发现this.Frame.CanGoBack = false How to solve it? 怎么解决?

From the code that you have posted we can see that this.Frame is actually the refering to the root frame of the application, which at the moment has only navigated to a single page ( MainPage ) (As defined in your App.xaml.cs ). 从您发布的代码中我们可以看到this.Frame实际上是this.Frame应用程序的框架,目前只导航到单个页面( MainPage )(如App.xaml.cs中所定义) )。 Thus there is no page that it can go back to ( this.Frame.CanGoBack = false ). 因此,没有可以返回的页面( this.Frame.CanGoBack = false )。


A little in depth explanation : 一点点深入解释:

If you go into App.xaml.cs file in your project, in the OnLaunched() method you will find the following code : 如果您进入项目中的App.xaml.cs文件,则在OnLaunched()方法中,您将找到以下代码:

rootFrame.Navigate(typeof(MainPage), e.Arguments);

Here the application, after launch will navigate the rootFrame to the MainPage . 这里的应用程序在启动后会将rootFrame导航到MainPage

When you use this.Frame from your MainPage it actually refers to the rootFrame , which at this moment has only navigated to the MainPage , thus it does not have any page that it can go back to , hence this.Frame.CanGoBack = false . 当您在MainPage使用this.Frame ,它实际上引用了rootFrame ,此时此rootFrame仅导航到MainPage因此它没有任何可以返回的页面 ,因此this.Frame.CanGoBack = false


Solution : 方案:

When you use a SplitView , in the content you should specify a Frame which you can use to navigate between different pages . 当您使用SplitView ,您应该在内容中指定一个Frame ,您可以使用该Frame在不同页面之间导航。 Thus your app will look something like this : 因此,您的应用程序将如下所示:

Here Red rectangle is used to show the rootFrame where as Blue is used to show the Frame which you have to define in your SplitView content. 此处红色矩形用于显示rootFrame ,其中Blue用于显示您必须在SplitView内容中定义的Frame 在此输入图像描述

For this, you need to make minor modifications to your code something like this : 为此,您需要对代码进行少量修改,如下所示:

XAML XAML

<Page
 .....
 .....
   <SplitView Name="MySplitView"
    .....>
       <SplitView.Pane>
         .....                   
        </SplitView.Pane>
        <SplitView.Content>
           <Frame x:Name="appFrame"></Frame>
        </SplitView.Content>
   </SplitView>    
</Page>

C# C#

private void IconsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (ShareListBoxItem.IsSelected)
       appFrame.Navigate(typeof(page1));//navigating to page1
    else if (FavoritesListBoxItem.IsSelected)
       appFrame.Navigate(typeof(page2));//navigating to page2
}
private void Back_Click(object sender, RoutedEventArgs e)
{
   if (appFrame.CanGoBack)
      appFrame.GoBack();
}

Hope this helps..! 希望这可以帮助..!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM