帧后导航

时间:2018-10-17 08:57:27

标签: uwp xbox

如何在uwp应用中导航到后框架?我有一个框架,它在该框架中加载三页。因此,当我单击“后退”按钮时,我需要查看该框架中加载的上一页。请帮忙。在代码中,我需要将页面加载到大型机中

        <Image Source="/Assets/Images/PURlogo_large.png" HorizontalAlignment="Left"  Margin="70,950" Width="212" Height="78"/>
            </Grid>
        </StackPanel>
        <Frame x:Name="MainFrame" Grid.Column="1" Content="{Binding FrameData,Mode=OneWay}" >
        </Frame>
        <Grid Background="Red" Visibility="Collapsed" x:Name="testgrid">
            <TextBlock Text="hello world"></TextBlock>
        </Grid>
    </Grid>

在App.xaml.cs中:我有一种处理后退按钮的常用方法。

private bool On_BackRequested()
     {
        Frame rootFrame = Window.Current.Content as Frame;


        if (rootFrame.Content is HomeView homeview)
        {
           // here I do nothing as it is handled by the back method in homeview.xaml.cs
        }

        if (rootFrame.Content is MyPageView myPage)
        {
            if (rootFrame.CanGoBack)
            {
                //Here its going back to the page instead of the frame.
            }

        }

        else if (rootFrame.CanGoBack)
        {
            rootFrame.GoBack();
            return true;
        }
        return false;

    }

这是在框架所在的homeview.xaml.cs中。效果很好。

private void OnBackRequested(object sender, BackRequestedEventArgs e)
    {
        if (MainFrame.CanGoBack)
        {
            e.Handled = true;
            MainFrame.GoBack();
        }
    }

我的导航就是这样。 page1-> page2(包含框架)-> framepage 1-> framepage2-> page 3

一旦我从第3页单击返回,而不是转到第2页第二帧页面(framepage2),它将转到具有第一帧页面的页面2。那么如何使用上面app.xaml.cs

中的一种常见方法来解决此问题

1 个答案:

答案 0 :(得分:0)

XAML SecondPage

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <Grid Grid.Row="0">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
            <ColumnDefinition Width="Auto" />
        </Grid.ColumnDefinitions>
        <Button x:Name="FrameBackButton" Grid.Column="0" Content="Back" Height="55" Width="255" Margin="5" Click="FrameBackButton_Click"/>
        <Button x:Name="Frame2Button" Grid.Column="1" Content="Load Frame2" Height="55" Width="255" Margin="5" Click="Frame2Button_Click"/>
        <Button x:Name="Frame3Button" Grid.Column="2" Content="Load Frame3" Height="55" Width="255" Margin="5" Click="Frame3Button_Click"/>
    </Grid>

    <Frame x:Name="MainFrame" Grid.Row="1"/>
</Grid>

C#第二页

public sealed partial class SecondPage : Page
{
    public SecondPage()
    {
        this.InitializeComponent();
        MainFrame.Navigated += MainFrame_Navigated;
        this.Loaded += SecondPage_Loaded;

    }

    private void MainFrame_Navigated(object sender, NavigationEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        foreach (var item in rootFrame.BackStack.ToList())
        rootFrame.BackStack.Remove(item);
    }

    private void SecondPage_Loaded(object sender, RoutedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;
        frame.BackStack.RemoveAt(frame.BackStackDepth - 1);
        MainFrame.Navigate(typeof(Frame1));
    }        

    private void Frame2Button_Click(object sender, RoutedEventArgs e)
    {
        if (MainFrame.CurrentSourcePageType != typeof(Frame2))
        {
            MainFrame.Navigate(typeof(Frame2));
        }
    }

    private void Frame3Button_Click(object sender, RoutedEventArgs e)
    {
        if (MainFrame.CurrentSourcePageType != typeof(Frame3))
        {
            MainFrame.Navigate(typeof(Frame3));
        }
    }

    private void FrameBackButton_Click(object sender, RoutedEventArgs e)
    {
        Frame frame = Window.Current.Content as Frame;

        if (MainFrame.Content is Frame1)
        {
            frame.Navigate(typeof(MainPage));
        }
        else
        {                
            if (MainFrame.CanGoBack)
                MainFrame.GoBack();
        }
    }

}

Sample