In my previous post, we discussed on how to create a Windows Phone 7 application and what are the different parts of the MainPage.xaml. We also created a small and simple HelloWorld application as the quick start. I guess, now we are comfortable to create a basic application with a good UI (if you already know how to design a XAML page).

 

It's time to explore more in depth about the page. In this post, we will discuss about the base class called "Page". Read to know more about it.

 

Index - Windows Phone 7 (Mango) Tutorial

 

Introduction to Page

Like "MainPage.xaml", every Windows Phone 7 page inherits from "PhoneApplicationPage" by default, which is part of Microsoft.Phone.Controls namspace. PhoneApplicationPage itself derives from the base class called "Page", part of System.Windows.Controls namspace. Every "Page" is nothing but a derived class of UserControl and if you are already familiar with the UserControl hierarchy, you know that the most topper class is the DependencyObject.

 

To make it simpler for you to understand, here is the complete hierarchy of a Windows Phone 7 Page:

 

image

 

 

We will discuss on "PhoneApplicationPage" tomorrow. In this post, we will start discussing on the base class "Page". Here is the meta data of the Page class implementation:

 
namespace System.Windows.Controls
{
    public class Page : UserControl
    {
        public NavigationContext NavigationContext { get; }
        public NavigationService NavigationService { get; }
        public string Title { get; set; }
        public NavigationCacheMode NavigationCacheMode { get; internal set; }
        protected virtual void OnFragmentNavigation(FragmentNavigationEventArgs e);
        protected virtual void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e);
        protected virtual void OnNavigatingFrom(System.Windows.Navigation.NavigatingCancelEventArgs e);
        protected virtual void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e);
    }
}

 

From the above meta data it is clear that, every Page consists of a Title. This is nothing but the Page Title that we discussed on yesterday's demo. You can change the Page title as per your requirement.

 

Every page consists of three more read only properties called "NavigationContext", "NavigationService" and "NavigationCacheMode". Let's start describing each one of them.

 

 

What is NavigationContext?

NavigationContext is a sealed class placed under System.Windows.Navigation namespace. It contains a Disctionary object to store the QueryString parameter. Like ASP.Net page, you can pass and retrieve query string parameters to and from any page.

 
namespace System.Windows.Navigation
{
    public sealed class NavigationContext
    {
        public IDictionary<string, string> QueryString { get; }
    }
}

 

It is very useful if you want to pass any value between multiple connecting pages.

 

 

What is NavigationService?

Like NavigationContext, it is also a sealed class present in the System.Windows.Navigation namespace. It has many other properties and methods to use page navigations comfortably. Here is the meta data of the NavigationService class:

 
namespace System.Windows.Navigation
{
    public sealed class NavigationService
    {
        public Uri Source { get; set; }
        public Uri CurrentSource { get; internal set; }
        public bool CanGoForward { get; }
        public bool CanGoBack { get; }
        public IEnumerable<JournalEntry> BackStack { get; }
        ~NavigationService();
        public bool Navigate(Uri source);
        public void GoForward();
        public void GoBack();
        public void StopLoading();
        public JournalEntry RemoveBackEntry();
        public event NavigationFailedEventHandler NavigationFailed;
        public event System.Windows.Navigation.NavigatingCancelEventHandler Navigating;
        public event System.Windows.Navigation.NavigatedEventHandler Navigated;
        public event NavigationStoppedEventHandler NavigationStopped;
        public event FragmentNavigationEventHandler FragmentNavigation;
        public event EventHandler<JournalEntryRemovedEventArgs> JournalEntryRemoved;
    }
}

 

From the above code snippet, it is very easy to understand about the functionality of the class. Using the properties called "Source" and "CurrentSource" you can easily discover the source of the current page.

 

CanGoForward() and CanGoBack() returns boolean value, which tells you whether any page available in history to navigate forward and backward respectively.

 

Navigate() method navigates the current page to a different page supplied as parameter to the method. GoForward() and GoBack() instructs the OS to navigate to the next or previous page from the History. While loading your page, you can cancel the operation by calling the StopLoading() method from the navigation service.

 

 

What is NavigationCacheMode?

NavigationCacheMode is a enum property which describes whether to cache the page. The default value of it is Disabled. This is also part of System.Windows.Navigation namspace and contains three enum values as shown below:

 
namespace System.Windows.Navigation
{
    public enum NavigationCacheMode
    {
        Disabled,
        Required,
        Enabled,
    }
}

 

Set the NavigationCacheMode property to Disabled if a new instance must be created for each visit. Set NavigationCacheMode to Required if you want the page to cached regardless of the number of cached pages and set it to Enabled if you want the page to cached for a number of period until the number of cached pages exceeds the value of CacheSize.

 

Hope, this will help you to understand the basics of Windows Phone 7 Programming. Feel free to ask your queries and don't forget to share your thoughts and feedback. Suggestions are always welcome.

Have a question? Or, a comment? Let's Discuss it below...

Thank you for visiting our website!

We value your engagement and would love to hear your thoughts. Don't forget to leave a comment below to share your feedback, opinions, or questions.

We believe in fostering an interactive and inclusive community, and your comments play a crucial role in creating that environment.