通过UWP应用中的框架进行向后导航

时间:2018-10-16 09:42:32

标签: c# uwp xbox

我有一个UWP应用程序,该应用程序有3页,第三页包含一个可加载多个框架的框架标签。假设我导航到第1页->第2页->第3页->框架1->框架2->框架3。如果我按第3帧和第2帧的后退按钮,则会转到第2页,而不是第2帧和第1帧分别。我有一个通用的方法来处理后退按钮事件,它采用与第1,2页和第3页(即第3页)相同的根框架,并返回其后页第2页。因此,我该如何返回到框架根页面的背面。以下是我在App.xaml.cs中具有的常用back方法。请帮忙。

  protected override void OnLaunched(LaunchActivatedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += App_BackRequested;

        }
    }


    private void App_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
        e.Handled = On_BackRequested();
    }



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



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

public HomeView()
    {
        this.InitializeComponent();
        var vm = new HomeViewModel(new UserService(), new SubscriptionService(), new TransactionService(),new GameService());
        this.DataContext = vm;
        WalletBtn.Foreground = new SolidColorBrush(Colors.White);
        PaymentGrid = BuyModal;
        //PowerPassGameListGrid = PowerPassGameGrid;
       // SystemNavigationManager.GetForCurrentView().BackRequested += HomePage_BackRequested;
        PointBalance = wallet;
        //QueuedGames = QueueGame;
        //PowerPassSubscriptionGrid = PowerPass;
        SubmitPopUp = SuccessPopUp;
        RecentActivityGrid = RecentActivityModel;
        CurrentlyPickedPopUp = GamePickUp;
        PowerUpNavigationGrid = NavigationGrid;
    }
    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        string firstName = (string)ApplicationData.Current.LocalSettings.Values["FirstName"];
        string lastName = (string)ApplicationData.Current.LocalSettings.Values["LastName"];
        if (firstName != null && lastName != null)
        {
            welcomeMessage.Text = "Hi, " + firstName;
        }
    }
    public void MyWalletButton_Click(object sender, RoutedEventArgs e)
    {
        PowerPassBtn.Foreground = new SolidColorBrush(Colors.Gray);
        WalletBtn.Foreground = new SolidColorBrush(Colors.White);       
    }
    public void PowerPassButton_Click(object sender, RoutedEventArgs e)
    {
        PowerPassSelection();
    }

    //private void HomePage_BackRequested(object sender, BackRequestedEventArgs e)
    //{
    //    e.Handled = true;
    //    this.Frame.Navigate(typeof(LoginUsernameView));
    //}
    private void ButtonGotFocus(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = new SolidColorBrush(Colors.Green);
    }

    private void ButtonLostFocus(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = new SolidColorBrush(Colors.Transparent);
    }

    private void QueueButtonGotFocus(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = new SolidColorBrush(Colors.DeepSkyBlue);
    }

    private void QueueButtonLostFocus(object sender, RoutedEventArgs e)
    {
        (sender as Button).Background = new SolidColorBrush(Colors.Transparent);
    }

    private static T FindParent<T>(DependencyObject dependencyObject) where T : DependencyObject
    {
        var parent = VisualTreeHelper.GetParent(dependencyObject);
        if (parent == null) return null;

        var parentT = parent as T;
        return parentT ?? FindParent<T>(parent);
    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        NavigationParameterDTO parameter = e.Parameter as NavigationParameterDTO;
        if (parameter != null)
        {
            if (parameter.FrameName == "WelcomeView")
            {
                HomeViewModel vm = this.DataContext as HomeViewModel;
                vm.PowerPassWelcomeFrame.Execute(null);
                PowerPassSelection();
            }
            else if(parameter.FrameName == "CheckOut")
            {
                HomeViewModel vm = this.DataContext as HomeViewModel;
                vm.PowerPassGameCheckOutMethod.Execute(null);
                PowerPassSelection();
            }
            else if(parameter.FrameName== "SignUp")
            {
                HomeViewModel vm = this.DataContext as HomeViewModel;
                vm.PowerPassFrame.Execute(null);
                PowerPassSelection();
            }
            else
            {
                HomeViewModel vm = this.DataContext as HomeViewModel;
                vm.PowerPassHomeFrame.Execute(null);
                PowerPassSelection();
            }
        }
    }

Xaml

                   <Button Style="{StaticResource PowerUpButtonStyle}"  x:Uid="Profile"  FontSize="36" Margin="0,60,0,0" Name="SettingsBtn" XYFocusDown="{x:Bind SettingsBtn}" FontFamily="Segoe Pro" Foreground="Gray" FocusVisualPrimaryThickness="0,0,0,3" FocusVisualMargin="10,0">
                        <Button.Background>
                            <SolidColorBrush Opacity="0"/>
                        </Button.Background>
                        <Button.Resources>
                            <ResourceDictionary>
                                <ResourceDictionary.ThemeDictionaries>
                                    <ResourceDictionary x:Key="Light">
                                        <SolidColorBrush x:Key="ButtonForegroundPointerOver" Color="White"/>
                                    </ResourceDictionary>
                                </ResourceDictionary.ThemeDictionaries>
                            </ResourceDictionary>
                        </Button.Resources>
                    </Button>
                </StackPanel>
                <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>

1 个答案:

答案 0 :(得分:1)

使用Window.Current.Content为您提供应用程序的最根本Frame。如果要检查Page3当前是否打开并利用其框架,则必须执行以下操作:

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

    if (rootFrame.Content is Page3 page3 )
    {
         var innerFrame = page3.GetInnerFrame(); //implement this method in Page3
         if (innerFrame.CanGoBack)
         {
             innerFrame.GoBack();
             return true;
         }
    } 

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

GetInnerFrame方法将位于Page3代码的后面,并将仅返回页面上的框架。

上面的代码将仅返回Page3中的框架。如果进一步嵌套,则必须再次使用此框架的Content,依此类推。