3
votes

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 ?

2
It looks perfect. What's not working? Your app is closing? Do you need an else in that logic?Jerry Nixon

2 Answers

2
votes

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.

0
votes

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