简体   繁体   中英

Back button controlling for my Windows Phone 8.1 Silverlight App

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.

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

    }

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