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?

+3


source to share


3 answers


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

      

+2


source


You must have an instance NavigationService

because it is Navigate

not a static method. Try



private void rectangle2_Click(object sender, RoutedEventArgs e)
{
    NavigationService Navserv = new NavigationService();
    Navserv.Navigate(new Uri("/Jobsearch.xaml", UriKind.Absolute));               
}

      

+2


source


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

      

+2


source







All Articles