Navigation Service in WPF

喜夏-厌秋 提交于 2019-12-08 14:46:47

问题


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?


回答1:


I believe you are looking for:

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

navigationservice.goback




回答2:


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.



来源:https://stackoverflow.com/questions/36168840/navigation-service-in-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!