Windows Phone 7 (Mango) Tutorial - 2 - Know more about Page


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.


13 comments

  1. It was very interesting for me to read it. Thanx for it.

    ReplyDelete
  2. Described well. I certainly look forward to the next chapter.

    ReplyDelete
  3. Good article...
    But looking at the whole navigation service, I felt a compelling need to be able to tweak the navigation history stack. I should be able to clear the navigation stack of be able to define my entire navigation sequence.
    The need arises mainly because of lack of a good popup/child window in SL for WP7 !!

    ReplyDelete
  4. Hi Aseem,
    Probably the Page and View concept may solve this issue in WP7. I will describe it in my future chapter of the tutorial series. Also to get the whole stack, you can use the property called "BackStack". It returns an IEnumerator of the JournalEntry.

    ReplyDelete
  5. Well Explained. Thanks.

    ReplyDelete
  6. Hello !! thanx for the post, and i hope somebody can help me with this, i have a panoramaControl where a MediaElement playing a track is running, using NavigationService i go to another page when the user hits an element placed in some of the panorama items, but when navigation occurs, the MediaElement stops due to the ActivePage changes to the new Active Page, i can't set the NavigationCacheMode Property of main page (it's readOnly property), how could i allow my MediaElement keep working even when its container is no longer the active page?? Thanx for the help

    ReplyDelete
  7. Hi! How to set NavigationCacheMode property in C# or XAML?

    ReplyDelete
  8. NavigationCacheMode is a property of Page instance. You can get the value directly from the page.

    ReplyDelete
  9. Hi Kunal,

    By default NavigationCacheMode is set to disabled. I am trying to enable it.

    this.NavigationCacheMode = System.Windows.Navigation.NavigationCacheMode.Enabled;

    Unfortunately the property is internal to Assembly Microsoft.Phone.dll, v2.0.50727; so I can't set it. The declaration is this:
    public NavigationCacheMode NavigationCacheMode { get; internal set; }

    So the compiler tells me:
    "Error 1 Property or indexer 'System.Windows.Controls.Page.NavigationCacheMode' cannot be assigned to -- it is read only"

    Please let me know if i am missing something here.

    Regards,
    Simmy

    ReplyDelete
  10. Bro,

    Your are really Rocking. Thanks a million for sharing your Knowledge.One question Can mango pull market share from android and IPhone apps.What do you fell that what will be the mago market share in feature....

    Thanks,
    Naveen J
    navininbox@yahoo.com

    ReplyDelete
  11. hi
    i want to develop localalize application.the resource file contain images,videos,text files and these are retrieved based on the ui font in phone.how to do it.

    ReplyDelete
  12. Just decided to give windows phone programming a venture and I discovered your site with seems to make this venture not so daunting. Thank you for sharing your knowledge. I will be following from here on.

    Regards...

    ReplyDelete


 
© 2008-2014 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top