WPF this.NavigationService.Navigate

3.3k Views Asked by At

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);

1

There are 1 best solutions below

3
mm8 On

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>