Navigation Service in WPF

2019-06-12 19:19发布

I created a simple application in WPF and added one page. I also added one button to the main window, and when I click on it, it brings the page added. But now, I want to go back from the page to the main windows by using another button.

I tried to use:

MainMenu n = new MainMenu();
this.NavigationService.Navigate(n);

but I got this error:

Object reference not set to an instance of an object.

Any help?

2条回答
Explosion°爆炸
2楼-- · 2019-06-12 19:28

I believe you are looking for:

void backButton_Click(object sender, RoutedEventArgs e)
{
    if (this.NavigationService.CanGoBack)
    {
        this.NavigationService.GoBack();
    }
}

navigationservice.goback

查看更多
聊天终结者
3楼-- · 2019-06-12 19:29

I finally did it by adding a frame inside MainWindow.xaml and redirecting it to the page i want.

 private void button_Click(object sender, RoutedEventArgs e)
 {
    frame.NavigationService.Navigate(new Uri("Page1.xaml",UriKind.Relative));    
 }

frame is the name of my new frame.

查看更多
登录 后发表回答