Back button controlling for my Windows Phone 8.1 Silverlight App

221 views Asked by At

I am developing a Windows Phone 8.1 Silverlight app. In my app, I need to override the back button code.

So I tried this,

protected override void OnBackKeyPress(CancelEventArgs e)
    {
        //Navigating to this page when back button is pressed
        DisplayPage mynewPage = new DisplayPage();
        this.Content = mynewPage;

        e.Cancel = true;
    }

But this code is not working. What am I doing wrong? Please help me out.

EDIT:

When I place this code on MainPage, it works! But I don't want to place it there. I want to place it on other pages.

1

There are 1 answers

4
hildegard On BEST ANSWER

Remove "e.Cancel = true" (it cancels the navigation). Simply just navigate to the new page.

EDIT:

For navigating to another page, I would rather suggest using NavigationService. Check out this page for examples.

EDIT:

Code snippet:

    protected override void OnBackKeyPress(CancelEventArgs e)
    {
        //Navigating to this page when back button is pressed
        NavigationService.Navigate(new Uri("/DisplayPage.xaml", UriKind.Relative));

    }