UWP:Frame in Frame Back handeling

时间:2017-05-19 18:44:13

标签: c# xaml uwp back handle

在我的新UWP应用程序中,我在框架中有一个框架,我想要处理后退按钮。 视图看起来像这样:

View

注意:F = Frame,P = PAge,Red = Navigation,Blue = NavigationBack

我有一个可以导航到某些页面的母版页。其中一个页面有一个内部根框架,导航到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;
        }

    }

1 个答案:

答案 0 :(得分:0)

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

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

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