Click here to Skip to main content
15,895,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, guy. I am struggling with navigation in my phone app. I have added three blank pages to my project and managed to navigate forward for page to page but ....
I want a user when click the "back button" on the phone go back just one page and of course when he press it again go back to the main page. What happens is, that at the moment user exits the app when pressing the back button on the device.
This is the code I have got by default in method called:
C#
private void HardwareButtons_BackPressed(object sender, Windows.Phone.UI.Input.BackPressedEventArgs e)
{
    if (this.GoBackCommand.CanExecute(null))
    {
        e.Handled = true;
        this.GoBackCommand.Execute(null);
    }
}

This is what I have tried to put in the method instead, and what I found on internet but didn't work:
C#
if (Frame.CanGoBack)
{
    e.Handled = true;
    Frame.GoBack();
}

and this one (quite similar) but didn't work either :(
Frame rootFrame = Window.Current.Content as Frame;

if (rootFrame != null && rootFrame.CanGoBack)
{
    e.Handled = true;
    rootFrame.GoBack();
}

Can anyone help me with this, please?
Posted
Updated 21-Mar-15 8:43am
v3
Comments
Afzaal Ahmad Zeeshan 21-Mar-15 16:44pm    
Probably you're not having anything as navigation, instead you're trying to load new content in the page. That is why, upon pressing back the user is going to the homescreen from the start activity. Something like that.

You have use NavigationService.BackStack to run through the previous pages.

Here is good article that should get you started - How to navigate using the back stack for Windows Phone 8[^].
 
Share this answer
 
I have solution from someone who helped me on another forum. Pasting it in her so that it might help someone else.
Basically the thing is, when you add "BlankPage" to your project instead of "BasicPage" there is some things missing. Here is what You need to paste/type into your code behind of your added "BlankPage". Your page has to subscribe to
Windows.Phone.UI.Input.HardwareButtons.BackPressed += HardwareButtons_BackPressed;

In code below commented parts are optional, depends on what you want to use.

You will be needing reference to your "common" folder which contains NavigationHelper.cs and I assume You have it.

C#
using (yourAppName).Common;


XML
public sealed partial class ListPage : Page
{
    private NavigationHelper navigationHelper;
   // private ObservableDictionary defaultViewModel = new ObservableDictionary();

    public ListPage()
    {
 
        this.InitializeComponent();
        this.navigationHelper = new NavigationHelper(this);
        //this.navigationHelper.LoadState += this.NavigationHelper_LoadState;
       // this.navigationHelper.SaveState += this.NavigationHelper_SaveState;

    }
 
    public NavigationHelper NavigationHelper
    {
        get { return this.navigationHelper; }
    }
 
    /// <summary>
    /// Invoked when this page is about to be displayed in a Frame.
    /// </summary>
    /// <param name="e">Event data that describes how this page was reached.
    /// This parameter is typically used to configure the page.</param>

    //private void NavigationHelper_LoadState(object sender, LoadStateEventArgs e)
    //{
    //}

    //private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e)
    //{
    //}



I hope it will help someone. Have fun:)
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900