2

I'm quite new on C# programming and I have a problem on my code. I have created a button and applied on it an event click which open an other page of my project by the technology NavigationService.

This is the script:

private void click_login(object sender, RoutedEventArgs e)
{
    NavigationService nav = NavigationService.GetNavigationService(this); 
    nav.Navigate(new Uri("Window1.xaml", UriKind.RelativeOrAbsolute));
}

When I execute, I get this error:

The object reference is not set to an instance of an object with an InnerException null.

Can you help me please?

enrico.bacis
  • 30,497
  • 10
  • 86
  • 115
Kraenys
  • 297
  • 1
  • 5
  • 19
  • did you checked if nav is not null when your debugging this ? and here a some Remarks when [GetNavigationService returns null](http://msdn.microsoft.com/en-US/library/system.windows.navigation.navigationservice.getnavigationservice(v=vs.110).aspx) – Mark Aug 11 '14 at 07:26
  • You have seen right, nav in null. Do I have to initiate with "new"? – Kraenys Aug 11 '14 at 07:48
  • No, please check the link i've provided in the remarks section there is a description when the GetNavigationService-Method returns null. There's probably something wrong with the `this` you are handing over to the method. – Mark Aug 11 '14 at 08:18
  • This returns 2 things: WpfApplication3.MainWindow which only have "InitializeComponent();" for the moment and the second thing is my button in mainwindow.xaml Is that mean there's a conflict about what "this" mean? – Kraenys Aug 11 '14 at 08:39
  • Whats the type of your MainWindow ? Window? – Mark Aug 11 '14 at 08:57
  • Indeed, it's Window. Like this: public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } – Kraenys Aug 11 '14 at 09:01

1 Answers1

4

Your nav object is null, because you're trying to get the NavigationService for a WPF Window.

But for Navigation you need a Page (Navigation Overview on MSDN)

A Little Working example:

Create to Page's Page1.xaml, Page2.xaml

In the App.xaml change the StartupUri to StartupUri="Page1.xaml"

Page1 Xaml:

 <StackPanel>
     <TextBlock Text="Hello from  Page1" />
     <Button Click="Button_Click" Content="Navigate to page 2"></Button>
 </StackPanel>

Page1 cs:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService nav = NavigationService.GetNavigationService(this);
        nav.Navigate(new Uri("Page2.xaml", UriKind.RelativeOrAbsolute));
    }

Page2 Xaml:

 <StackPanel>
     <TextBlock Text="Hello from  Page2" />
     <Button Click="Button_Click" Content="Navigate to page 1"></Button>
 </StackPanel>

Page2 cs:

    private void Button_Click(object sender, RoutedEventArgs e)
    {
        NavigationService nav = NavigationService.GetNavigationService(this);
        nav.Navigate(new Uri("Page1.xaml", UriKind.RelativeOrAbsolute));
    }
Mark
  • 3,273
  • 2
  • 36
  • 54
  • Sorry for my late, i come back from eat. If i understand what you say, it's because i have created a window and not a page that i have a problem. I create a new project and try your codes, i come to you quickly to tell you what happen. Thank you for your answer – Kraenys Aug 11 '14 at 10:53
  • Okay there it's works. Now i try to understand why. On my project, i want to navigate from a window, my main window to an other window, window1 I have tried to navigate from my main window to a page called Page1.xaml, same error. Have I to create an other page, containing the xaml code of the main window and redirecting the StartupUri to this page. (in order to get a page to page navigation?) – Kraenys Aug 11 '14 at 10:58
  • Okay now it works for my project. I have created a main page instead of a main window. Now i can navigate from the interface to the login by clicking on the button. I just have a last button, how can i call the next page like a pop-up and not like an other page? Thanks in advance. – Kraenys Aug 11 '14 at 11:05
  • With the NavigationService you can't navigate between Windows that's what the Page is for. If you want to have a popup the Navigation Service is not what you are looking for. There are several different ways how to open popups or dialogs in WPF for example [Window.ShowDialog](http://msdn.microsoft.com/en-us/library/system.windows.window.showdialog(v=vs.110).aspx) or [Popup Control](http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.popup(v=vs.110).aspx) – Mark Aug 11 '14 at 11:31