Handling Back Navigation Windows 10 (UWP)

前端 未结 5 741
礼貌的吻别
礼貌的吻别 2020-11-30 10:32

In my Xaml Page I\'ve got a Frame.

I\'m trying to have a backButton event to just navigate inside frame .

so I tried to use this piece of code



        
5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 11:01

    I think this is because you add HardwareButtons_BackPressed in your page instead on in app.xaml.cs.

    In app.xaml.cs :

    public App()
    {
        this.InitializeComponent();
        this.Suspending += this.OnSuspending;
    
        HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }
    
    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        Frame rootFrame = Window.Current.Content as Frame;
    
        if (rootFrame != null && rootFrame.CanGoBack)
        {
            e.Handled = true;
            rootFrame.GoBack();
        }
    }
    

    Now, back button of your phone will work on any pages.

    And then, if you want to add a particular button doing back in any page :

    In the particular page (or each pages if you want) :

    public void btn_return_Tapped(object sender, TappedRoutedEventArgs e)
            {
                Frame rootFrame = Window.Current.Content as Frame;
    
                if (rootFrame != null && rootFrame.CanGoBack)
                {
                    e.Handled = true;
                    rootFrame.GoBack();
                }
            }
    

    Source : http://windowsapptutorials.com/tips/general-tips/handling-the-back-button-in-a-windows-phone-8-1-app/

提交回复
热议问题