0

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?

Pang
  • 9,564
  • 146
  • 81
  • 122
Kayode
  • 3
  • 1
  • 6

2 Answers2

0

I believe you are looking for:

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

navigationservice.goback

Kory Gill
  • 6,993
  • 1
  • 25
  • 33
  • Hi @Kory Gill..I tried what you said but still got Object reference not set to an instance of an object exception – Kayode Mar 29 '16 at 23:22
0

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.

Alfie
  • 1,903
  • 4
  • 21
  • 32
Kayode
  • 3
  • 1
  • 6