C# UWP应用程序中的帧反向导航

C# UWP应用程序中的帧反向导航,c#,uwp,xbox,C#,Uwp,Xbox,我有一个UWP应用程序,它有3个页面,第三个页面包含一个框架标签,可以加载多个框架。假设我导航到第1页->第2页->第3页->第1帧->第2帧->第3帧。如果我从第3帧和第2帧按下后退按钮,它将分别转到第2页而不是第2帧和第1帧。我有一个常用的方法来处理back button事件,它取根帧,它与第3页的第1、2和3帧相同,然后返回到第2页。那么,我怎样才能回到框架而不是根页面呢。下面是我在App.xaml.cs中使用的常用back方法。请帮忙 protected override void

我有一个UWP应用程序,它有3个页面,第三个页面包含一个框架标签,可以加载多个框架。假设我导航到第1页->第2页->第3页->第1帧->第2帧->第3帧。如果我从第3帧和第2帧按下后退按钮,它将分别转到第2页而不是第2帧和第1帧。我有一个常用的方法来处理back button事件,它取根帧,它与第3页的第1、2和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();
            }
        }
    }
protectedoverride void OnLaunched(启动ActivatedEventargs e)
{
Frame rootFrame=Window.Current.Content作为Frame;
Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested+=App\u BackRequested;
}
}
私有void App_BackRequested(对象发送方,Windows.UI.Core.BackRequestedEventArgs e)
{
e、 Handled=On_backrequest();
}
后台请求的私有布尔值()
{
Frame rootFrame=Window.Current.Content作为Frame;
if(根框架.CanGoBack)
{
rootFrame.GoBack();
返回true;
}
返回false;
}
公共HomeView()
{
this.InitializeComponent();
var vm=新的HomeViewModel(新的UserService(),新的SubscriptionService(),新的TransactionService(),新的GameService());
this.DataContext=vm;
WalletBtn.Foreground=新的SolidColorBrush(Colors.White);
PaymentGrid=BuyModal;
//PowerPassGameListGrid=PowerPassGameGrid;
//SystemNavigationManager.GetForCurrentView().BackRequested+=主页\u BackRequested;
点平衡=钱包;
//QueuedGames=QueueGame;
//PowerPassSubscriptionGrid=PowerPass;
submitpoup=成功弹出;
RecentActivityGrid=RecentActivityModel;
CurrentlyPickedPopUp=游戏拾取;
PowerUpNavigationGrid=NavigationGrid;
}
已加载私有无效页面(对象发送方,路由目标e)
{
string firstName=(string)ApplicationData.Current.LocalSettings.Values[“firstName”];
字符串lastName=(字符串)ApplicationData.Current.LocalSettings.Values[“lastName”];
if(firstName!=null&&lastName!=null)
{
welcomeMessage.Text=“嗨,”+名字;
}
}
公共作废MyWalletButton_单击(对象发送者,路由目标)
{
PowerPassBtn.Foreground=新的SolidColorBrush(Colors.Gray);
WalletBtn.Foreground=新的SolidColorBrush(Colors.White);
}
public void PowerPassButton_单击(对象发送器,路由目标)
{
PowerPassSelection();
}
//私有无效主页\u BackRequested(对象发送方,BackRequestedEventArgs e)
//{
//e.已处理=正确;
//this.Frame.Navigate(typeof(LoginUsernameView));
//}
私有void按钮焦点(对象发送方,路由目标)
{
(发送者作为按钮)。背景=新的SolidColorBrush(Colors.Green);
}
私有void按钮lostfocus(对象发送方,路由目标e)
{
(发送者作为按钮)。背景=新的SolidColorBrush(颜色。透明);
}
private void QueueButtonGotFocus(对象发送方,RoutedEventTarget e)
{
(发送者作为按钮)。背景=新的SolidColorBrush(Colors.DeepSkyBlue);
}
private void QueueButtonLostFocus(对象发送方,RoutedEventArgs e)
{
(发送者作为按钮)。背景=新的SolidColorBrush(颜色。透明);
}
私有静态T FindParent(DependencyObject DependencyObject),其中T:DependencyObject
{
var parent=VisualTreeHelper.GetParent(dependencyObject);
if(parent==null)返回null;
var parentT=作为T的父项;
返回父母??找到父母(父母);
}
受保护的覆盖无效OnNavigatedTo(NavigationEventArgs e)
{
基地。导航到(e);
NavigationParameterDTO参数=e.参数作为NavigationParameterDTO;
if(参数!=null)
{
如果(parameter.FrameName==“WelcomeView”)
{
HomeViewModel vm=this.DataContext作为HomeViewModel;
PowerPassWelcomeFrame.Execute(null);
PowerPassSelection();
}
else if(parameter.FrameName==“签出”)
{
HomeViewModel vm=this.DataContext作为HomeViewModel;
PowerPassGameCheckOutMethod.Execute(null);
PowerPassSelection();
}
else if(parameter.FrameName==“注册”)
{
HomeViewModel vm=this.DataContext作为HomeViewModel;
PowerPassFrame.Execute(null);
PowerPassSelection();
}
其他的
{
HomeViewModel vm=this.DataContext作为HomeViewModel;
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>

使用
Window.Current.Content
提供应用程序的根
框架。如果要检查
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
code-behind中,只返回页面上的帧