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:
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:
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.
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:
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:
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.