如何在Windows 10通用应用程序开发中使用硬件后退按钮限制导航到某些页面?

时间:2015-08-01 07:14:30

标签: c# windows xaml

我关注http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps

我的导航如下: 登录 - > A-> B-&℃。 当我按下C的后退按钮时 它应该像C-> B-> A一样导航,不应该导航到登录页面A。

    SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Visible;
    SystemNavigationManager.GetForCurrentView().BackRequested += Util.Navigation_BackRequested;

    public static void Navigation_BackRequested(object sender, BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        if (rootFrame.Equals(typeof(MeetingsList)))
        {
            Util.debugLog("RESTRICT BACK ");
        }
        else 
        {
            if (rootFrame.CanGoBack)
            {
                e.Handled = true;
                rootFrame.GoBack();
            }
        }
        SystemNavigationManager.GetForCurrentView().AppViewBackButtonVisibility = AppViewBackButtonVisibility.Collapsed;

    }

2 个答案:

答案 0 :(得分:0)

您可以在从登录页面A导航时调用rootFrame.RemoveBackEntry()。这应该RemoveBackEntry()删除导航堆栈中的最后一个条目。因此,您可以在OnNavigatedTo()的第A页上执行此操作。但是你应该检查你是来自登录还是Page B!

答案 1 :(得分:0)

默认情况下,UWP开发不启用后退按钮。 Frist U需要跟进以下链接描述如何启用UWP App开发的后退按钮

http://www.wintellect.com/devcenter/jprosise/handling-the-back-button-in-windows-10-uwp-apps

然后您可以使用以下代码提示限制导航

    private void App_BackRequested(object sender, Windows.UI.Core.BackRequestedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
        bool status = false;
        if(rootFrame == null)
        {
            return;
        }
        if (rootFrame.SourcePageType.Name.Equals("POSummary") || rootFrame.SourcePageType.Name.Equals("AutoBinAllocation"))
        {
            e.Handled = true;
            status = true;

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