WP7 Tutorial : 02 - Know more about Page

Sep 12, 2012 Posted by Lara Kannan
It's time to explore more in depth about the page. In this post, we will discuss about the base class called "Page".

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

Source : www.kunal-chowdhury.com/

Hope, this will help you to understand the basics of Windows Phone 7 Programming!

Share

Post a Comment