简体   繁体   中英

How to add page into Navigation stack?( Windows Phone)

when I navigate to Page1.xaml, I have an empty navidation stack, what I need to add into

protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e){}

to add Page2.xaml into Navigation stack (I need to navidate into Page2.xaml only when I press go back button)

If I understand correctly, you want to navigate to Page2.xaml when the user press the Back button, correct?

You'll have to use the BackKeyPressed event to make that work, like so:

public MainPage()
{
    InitializeComponent();

    this.BackKeyPress += new EventHandler<System.ComponentModel.CancelEventArgs>(MainPage_BackKeyPress);
}

void MainPage_BackKeyPress(object sender, System.ComponentModel.CancelEventArgs e)
{
    e.Cancel = true;

    Dispatcher.BeginInvoke(() =>
    {
        NavigationService.Navigate(new Uri("/Page2.xaml", UriKind.Relative));
    });
}

But please be advised that changing the default behavior of the Back button may lead to fail app certification!

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