In the given code is NavigationService a class or an object?

496 Views Asked by At
void RecordButton_Click(object sender, EventArgs e)
{
    NavigationService.Navigate(new Uri("/RecordAudio.xaml",UriKind.Relative));
}

In the above code , is NavigationService a class or is it an object of type NavigationService ? .

if it is a class . then is Navigate() a static method ?

and

if it is an object . Why have we not instantiated NavigateService class using the new operator ?

3

There are 3 best solutions below

0
On

As @VahidNd said - use F12 (if you use VS). To clarify it's like this:
PhoneApplicationPage (which you use) base class is Page:

public class PhoneApplicationPage : Page
{
 // content
}

public class Page : UserControl
{
    public NavigationCacheMode NavigationCacheMode { get; internal set; }
    public NavigationContext NavigationContext { get; }
    public NavigationService NavigationService { get; }
    public string Title { get; set; }

    protected virtual void OnFragmentNavigation(FragmentNavigationEventArgs e);
    protected virtual void OnNavigatedFrom(NavigationEventArgs e);
    protected virtual void OnNavigatedTo(NavigationEventArgs e);
    protected virtual void OnNavigatingFrom(NavigatingCancelEventArgs e);
}

And you can see that it has a property NavigationService of type NavigationService which is a clss in System.Windows.Navigation.
You can see these dependences when you use this F12 key - just set cursor's position on type/property/what you want, and hit the key.

3
On

NavigationService is a non static class and so is its method Navigate. And as the method is non static, you need to create an object of the class. But the class NavigationService gets instantiated automatically when the app is run and gets attached as a property to the Frame object and hence available commonly to all the Page Object in a single frame.

refer

For Understanding The Difference between Page Frame Content and Role of the class NavigationService.

Link to a related question One

Link to a related question Two

1
On

From what i know: The NavigationService class "belongs" to the recent page you're on and it is never used "on its own". You use its methods instead, like with navigate. The page gets informed when sth. new is to display.