locked
Frame.CanGoBack RRS feed

  • Question

  • Hi,

    I am making an app with some pages,

    In this example I want to navigate from Main to About, and then back; It works.

    when I navigate to another window and then back to main my "are you sure you want to exit" popup appears, my guess; thinks that the frame.cangoback is not there or something.

    My code:

     private async void HardwareButtons_BackPressed(object sender, BackPressedEventArgs e)
            {
                e.Handled = true;
                //This is where all your 'override backkey' code goes
                //You can put message dialog and/or cancel the back key using e.Handled = true;
                if (!this.Frame.CanGoBack)
                {
                    var messgeDialog = new MessageDialog("Are you sure you want to close Screen Texter?", "Are you sure?");
                    messgeDialog.Commands.Add(new UICommand("yes"));
                    messgeDialog.Commands.Add(new UICommand("no"));
                    messgeDialog.DefaultCommandIndex = 0;
                    messgeDialog.CancelCommandIndex = 1;
                    var result = await messgeDialog.ShowAsync();
                    if (result.Label.Equals("yes"))
                    {
                        Application.Current.Exit();
                    }
                    else
                    {
                        e.Handled = true;
                    }
                }
            }

    Any idea how to fix this, so it ONLY appears on my MainWindow etc?

    -Vincent

    Wednesday, February 25, 2015 1:47 PM

Answers

  • Solved the issue by:

    1. Adding a protected override OnNavigatedFrom to remove the BackPressed button.
    2. On my other window, I did the same. The BackPressed handles the event and does a Frame.GoBack();

    • Marked as answer by Vincent Gio Thursday, February 26, 2015 1:34 PM
    Thursday, February 26, 2015 1:34 PM

All replies

  • Make sure you do not clear the frame's backstack or remove entries from it somewhere.

    And by the way, apps should not display any kind of UI to actually close them, so calling App.Exit() will likely cause the marketplace to reject you app.

    Wednesday, February 25, 2015 2:48 PM
  • Hi, thank you for replying!

    Would you be kind to give me a sample of how to do this properly?

    Am still learning this environment so apologies.

    -Vincent

    Wednesday, February 25, 2015 3:18 PM
  • As long as you don't mess around with the backstack somewhere, your code sample will work fine. So you need to check your code.
    Thursday, February 26, 2015 7:36 AM
  • hi, I have no idea what a backatack is, so how would I inspect this issue?
    Thursday, February 26, 2015 7:38 AM
  • Where in the code is the event handler for the back button?

    In which class?

    Thursday, February 26, 2015 12:41 PM
  • I put it in my MainWindow. If that is what you're asking for.
    Thursday, February 26, 2015 12:56 PM
  • Solved the issue by:

    1. Adding a protected override OnNavigatedFrom to remove the BackPressed button.
    2. On my other window, I did the same. The BackPressed handles the event and does a Frame.GoBack();

    • Marked as answer by Vincent Gio Thursday, February 26, 2015 1:34 PM
    Thursday, February 26, 2015 1:34 PM