Why am I getting an error on windows.navigation?
I have the following code
private void rectangle2_Click(object sender, RoutedEventArgs e)
{
NavigationService.Navigate(new Uri("/Jobsearch.xaml", UriKind.Absolute));
}
I have run a button in windowsphonecontrol page.xaml, I want when I click on this button, go to another page, but I get the error:
Object reference is required for non-static field, method or property 'System.Windows.Navigation.NavigationService.Navigate (System.Uri)'
Why?
source to share
NavigationService
is not a class static
and is Navigate
not static
.
You cannot call this method without an instance of the class NavigationService
. You need to instantiate the class NavigationService
and call it Navigate
with a parameter new Uri("/Jobsearch.xaml", UriKind.Absolute)
.
As the error message says:
Object reference required for non-static field, method, or Property
private void rectangle2_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/Jobsearch.xaml", UriKind.Absolute));
}
source to share
A static method is a method that can be called without instantiating an object:
YourClass.YourStaticMethod();
An inactive method acts on the object you call it on:
YourClass yourObject = new YourClass(); // creating an instance
yourObject.YourNonStaticMethod(); // Applies only to yourObject
source to share