C# UWP:帧对帧背向投球

C# UWP:帧对帧背向投球,c#,xaml,uwp,back,handle,C#,Xaml,Uwp,Back,Handle,在我的新UWP应用程序中,我想处理后退按钮的帧中有一帧。 视图如下所示: 注:F=框架,p=页面,红色=导航,蓝色=导航返回 我有一个母版页,可以导航到一些页面。其中一个页面有一个内部根框架,可以导航到p4 onload。p4可以导航到某些页面。 问题是,按照微软的导航方式,我只能处理根框架(f1)或内部框架(f2) 使用此代码: public P3() { this.InitializeComponent(); if (ApiInformation.I

在我的新UWP应用程序中,我想处理后退按钮的帧中有一帧。 视图如下所示:

注:F=框架,p=页面,红色=导航,蓝色=导航返回

我有一个母版页,可以导航到一些页面。其中一个页面有一个内部根框架,可以导航到p4 onload。p4可以导航到某些页面。 问题是,按照微软的导航方式,我只能处理根框架(f1)或内部框架(f2)

使用此代码:

public P3()
    {
        this.InitializeComponent();
        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            HardwareButtons.BackPressed += HardwareButtons_BackPressed;
        else
            Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested +=
        App_BackRequested;
    }
    ~P3()
    {
        if (ApiInformation.IsTypePresent("Windows.Phone.UI.Input.HardwareButtons"))
            HardwareButtons.BackPressed -= HardwareButtons_BackPressed;
        else
            Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested -=
         App_BackRequested;
    }

    private void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {


        if (Frame == null)
            return;

        // Navigate back if possible, and if the event has not 
        // already been handled .
        if (Frame.CanGoBack && e.Handled == false)
        {
            e.Handled = true;
            Frame.GoBack();
        }


    }

    private void App_BackRequested(object sender, BackRequestedEventArgs e)
    {
        if (Frame == null)
            return;

        // Navigate back if possible, and if the event has not 
        // already been handled .
        if (Frame.CanGoBack && e.Handled == false)
        {
            e.Handled = true;
            Frame.GoBack();
        }


    }
    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);
        F2.Navigate(typeof(P4));

        if (Frame.CanGoBack)
        {
            // Show UI in title bar if opted-in and in-app backstack is not empty.
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                AppViewBackButtonVisibility.Visible;
        }
        else
        {
            // Remove the UI from the title bar if in-app back stack is empty.
            SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility =
                AppViewBackButtonVisibility.Collapsed;
        }

    }

看起来您的页面3中有一个名为F2的框架。这是要用于向后导航的框架。因此,您需要更改后向导航


请注意,您使用的是
Frame
,这是页面本身的一个属性,可以访问页面的父框架。

注意:无需检查硬件后退按钮。订阅
backrequest
适用于硬件或软件后退按钮。导航可通过任一帧处理。只需确保将e.Handled设置为true,并检查两者的e.Handled。看起来你是在第三页检查这个,但是你原来的相框呢?@ShawnKendrot我都做了。但是没有结果,你想导航回两个帧还是一个帧?导航回哪个帧?你能分享第一帧的代码吗?@ShawnKendrot我希望两帧都能导航回来。如果你看图表,如果我们在F2的P6,如果我们向后导航,我们应该在P4,如果我们再次按下后退,我们应该在F1。我刚刚在P3中复制了上面相同的代码(当你按back键时,即使F2被导航到P6,你也会转到F1),而当我将代码复制到P4中时,我只能从P5&P6导航回P4。不是从P3到F1.public P3(){this.InitializeComponent();Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested+=OnBackRequested;}private void OnBackRequested(对象发送方,BackRequestedEventArgs e){if(F2==null)return;//如果可能,并且事件尚未处理,则返回//如果(F2.CanGoBack&&e.handled==false){e.handled=true;F2.GoBack();}否则{e.handled=true;Frame.GoBack();}我使用了该代码。它有时会工作,有时会崩溃:(我添加了这个选项,如果(e.Handled==false){e.Handled=true;Frame.GoBack();}它有时会从P6转到F1:|使用您发送的代码,我可以在F2页面内导航,但无法从P3(F2的主机)返回)到F1当我导航回F1并重新激活到p3,然后导航到F2到p5/p6时,我知道P2.CanGoBack等于False,backbackbackdepth为0:(
public P3()
{
    this.InitializeComponent();
    Windows.UI.Core.SystemNavigationManager.GetForCurrentView().BackRequested += OnBackRequested;
}

private void OnBackRequested(object sender, BackRequestedEventArgs e)
{
    if (F2 == null)
        return;

    // Navigate back if possible, and if the event has not 
    // already been handled .
    if (F2.CanGoBack && e.Handled == false)
    {
        e.Handled = true;
        F2.GoBack();
    }
}