简体   繁体   中英

BackButtonPressed issues in Windows Phone 8.1?

I have a popup window in my windows phone 8.1 runtime application.

While back button pressed and popup is opened in a page, the app should stay in the page itself, else it should go back. This is my concept. So, I coded like below:

    void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
    {
        if (PopupWindow.IsOpen)
        {
            PopupWindow.IsOpen = false;
            e.Handled = true;
        }
    }

Even if the popup windows is open in the page, the app goes to the previous page. I used the same logic in windows phone silverlight application and that worked.

NOTE: I'm using Basic Page.

What mistake actually I'm doing ?

Check two things:

  • by default in NavigationHelper , HardwareButtons_BackPressed lacks checking if the event was already handeled, try to improve it:

     private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e) { // if (this.GoBackCommand.CanExecute(null)) // this is as a default if (this.GoBackCommand.CanExecute(null) && !e.Handled) // add a check-up // ... rest of the code 
  • look at your App.xaml.cs file, and in App() there is HardwareButtons_BackPressed subscribed (check if subscribed method also navigates back):

     public App() { this.InitializeComponent(); this.Suspending += OnSuspending; // HardwareButtons.BackPressed += HardwareButtons_BackPressed; // this line also could fire Frame.GoBack() (as default project template) // of course check what is in the above method } 

Also remeber that events are fired in the order you have subscribed them and for example Navigation helper subscribes in Loaded event. If you subscribe after then the navigation will be first. You may subscribe before or maybe use a flag.

I resolve in thi way

    protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;
    }

    protected virtual void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
    {
        e.Handled = true;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM