1

I am learning a WPF tutorial from Microsoft website which is following:

https://learn.microsoft.com/en-us/dotnet/framework/wpf/getting-started/walkthrough-my-first-wpf-desktop-application#add-code-to-handle-events

I am getting the following error from my code:

private void Button_Click(object sender, RoutedEventArgs e)
{
    //View Expense Report
    ExpenseReportPage expenseReportPage = new ExpenseReportPage(); //Line 1
    this.NavigationService.Navigate(expenseReportPage); // Line 2
}

Line 2 in above code gives following error for NavigationService:

'invalid-global-code' does not contain a definition for 'NavigationService' and no accessible extention method 'NavigationService' accepting the first argument of type '' could be found (are you missing a using directive or an assembly reference ?)

I have added the using System.Windows.Navigation; I think the problem is coming from 'this' part of this.NavigationService.Navigate(expenseReportPage);

Mikev
  • 2,012
  • 1
  • 15
  • 27
sheraz yousaf
  • 79
  • 3
  • 7

1 Answers1

0

Change the Window element to a NavigationWindow element, or add a Frame element to your window and access its NavigationService property:

frame.NavigationService.Navigate(expenseReportPage); // Line 2

XAML:

<Window ...>
    <Frame x:Name="frame" />
</Window>
mm8
  • 163,881
  • 10
  • 57
  • 88
  • I have changed the Window element to NavigationWindow as instructed in the tutorial it still does not work. I can not add Frame attribute to NavigationWindow element as that option is not available. Please suggest. – sheraz yousaf Feb 18 '19 at 18:28
  • FIXED: I had this.NavigationService... block outside NavigationWindow class in code-behind. I moved the block inside this class and it worked. – sheraz yousaf Feb 18 '19 at 18:49
  • You can add a `Frame` element to a window as I demonstrated in my answer. If you have a `NavigationWindow`, you shouldn't add a `Frame`. – mm8 Feb 19 '19 at 12:02