locked
used HardwareButtons_BackPressed handler

    Question

  • hi

    how i can used HardwareButtons_BackPressed handler  just when i was in MainPage?

    i want when press HardwareBackButton and i was in MainPage(not other Page) my app close.

    i write this code to my MainPage:

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
            {
                Application.Current.Exit(); 
            } 

    but when i pressed HardwareBackButton my app closed in any other page.

    i want just i was in MainPage then Close app.

    tank you very much


    Amir Ameri

    Thursday, January 1, 2015 9:31 AM

Answers

  • Hi,You need to remove all navigation back entry by using adding following code in MainPage 

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)

            {

            while (NavigationService.CanGoBack)
                {
                    NavigationService.RemoveBackEntry();
                }

    }

    If my reply answers your question, please mark this post as answered.

    • Marked as answer by AMIR ameri Tuesday, January 6, 2015 7:23 PM
    Friday, January 2, 2015 5:11 AM

All replies

  • Hi Amir,

    Per my understanding, close the app is the default behavior when clicking the Back HardwareButton. In this scenario, you should handle HardwareBUttons_BackPressed event in other page and let them navigate to previous page or particular page. Code may look like the following.

    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    
            {
    
                Frame rootFrame = Window.Current.Content as Frame;
    
                if(rootFrame != null && rootFrame.CanGoBack)
    
                {
    
                    //Previous page
    
                    //rootFrame.GoBack();
    
                    //e.Handled = true;
    
    
                    //Particular page
    
                    rootFrame.Navigate(typeof(Two));
    
                    //Interrupt the default behaior
    
                    e.Handled = true;
    
                }
    
            }
    

    Regards,


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Friday, January 2, 2015 2:35 AM
    Moderator
  • Hi,You need to remove all navigation back entry by using adding following code in MainPage 

    private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)

            {

            while (NavigationService.CanGoBack)
                {
                    NavigationService.RemoveBackEntry();
                }

    }

    If my reply answers your question, please mark this post as answered.

    • Marked as answer by AMIR ameri Tuesday, January 6, 2015 7:23 PM
    Friday, January 2, 2015 5:11 AM