Покадровая навигация - PullRequest
       89

Покадровая навигация

0 голосов
/ 17 октября 2018

Как я могу перейти к задней рамке в приложении 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 голосов
/ 04 ноября 2018

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();
        }
    }

}

Образец

...