MOBILE

Windows Phone 7 : Working with Controls and Themes - Understanding Frame and Page Navigation

2/12/2011 3:59:31 PM
To navigate from screen to screen in a Windows Phone 7 application, an understanding of the PhoneApplicationFrame and PhoneApplicationPage controls is important. There is only one PhoneApplicationFrame available to a Windows Phone 7 application; this frame reserves space for the System Tray and the Application Bar, as well as the content area where PhoneApplicationPage controls live. You can create as many different pages as needed and then navigate to those pages from the frame. See Figure 1 to see how the controls are placed in the phone.
Figure 1. PhoneApplicationFrame and PhoneApplicationPage

To navigate from page to page within your application, use the NavigationService class. This class exposes methods to navigate to pages given a URI, as well as to go back to the previous page. The following walkthrough illustrates the use of the NavigationService class.

1. Creating a User Interface for NavigationTest Project

The NavigationTest project will contain two XAML pages (MainPage.xaml and Page1.xaml) and will navigate between the two.

  1. Launch Visual Studio 2010 Express and select the Windows Phone Application template. Change the Project Name to "NavigationTest," select OK, and Visual Studio will set up a new project.

  2. Right-click the project name in Solution Explorer and select Add => New Item => Windows Phone Portrait Page. Accept the default name of Page1.xaml and press OK.

  3. Open MainPage.xaml in Design View. From the toolbox, drag and drop the HyperlinkButton control. With that control selected, press F4 to display its properties and change the Contents to be "Go to Page1."

  4. Open Page1.xaml in the design view, and add a button to that page from the toolbox. Edit the Contents of the button to say "Go Back to MainPage."

  5. Add a TextBlock underneath the button on Page1.xaml. This TextBlock will be used to show the parameters passed in to this page.

In the next section, we will use NavigationService to navigate between pages.

2. Adding Navigation Code

When the user clicks the "Go to Page 1" hyperlink, you will be using NavigationService to move to Page1.

  1. Open MainPage.xaml and double-click the hyperlink on that page. Implement the hyperlinkButton1_Click event handler with the following code:

    private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.Navigate(new Uri("/Page1.xaml", UriKind.Relative));

    }

  2. Open Page1.xaml and double-click the button on that page. Implement the button1_Click event handler with the following code:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.GoBack();
    }

  3. Press F5 to run the application. Now when you click the hyperlink on MainPage.xaml, you are taken to Page1.xaml. When you click the button on Page1.xaml, you are taken back to MainPage.xaml. In the next section, we will enhance this application slightly to pass parameters between the pages.

3. Adding Code to Pass Parameters Between Pages

In the previous section, you learned how to successfully navigate from page to page. In this section, you will see how you can pass parameters from one page to another.

  1. Open MainPage.xaml.cs and change the hyperlinkButton1_Click event handler to the following:

    private void hyperlinkButton1_Click(object sender, RoutedEventArgs e)
    {
    NavigationService.Navigate(new Uri("/Page1.xaml?message=Hello,World",
    UriKind.Relative));
    }


Here, you are passing the hard-coded string "Hello, world" to Page1.xaml for processing.

  1. In Page1.xaml, you will try to read the query string passed from the prior pages to see if there are non-empty values. Open Page1.xaml.cs and add the following code to that file:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs
    e)
    {
    base.OnNavigatedTo(e);
    string msg = "";
    if (NavigationContext.QueryString.TryGetValue("message", out msg))
    textBlock1.Text = msg;
    }


  2. Press F5 to run the application. Now, if you press the hyperlink from MainPage.xaml, you should see the "Hello, world" message displayed on Page1.

Other  
  •  Windows Phone 7 : Working with Controls and Themes - Panorama and Pivot Controls
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 5) - Windows Mobile & BlackBerry
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 4) - Windows Mobile & BlackBerry
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 3) - webOS & Android
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 2) - iPhone, iPod, and iPad
  •  Programming the Mobile Web : Widgets and Offline Webapps - Platforms (part 1) - Symbian/Nokia
  •  Programming the Mobile Web : Widgets and Offline Webapps - Standards
  •  Mobile Application Security : BlackBerry Security - Networking
  •  Mobile Application Security : BlackBerry Security - Local Data Storage
  •  Themes on Windows Phone 7 Devices (part 2) - Changing the Theme & Detecting the Currently Selected Theme
  •  Themes on Windows Phone 7 Devices (part 1) - Applying a Theme
  •  Programming the Mobile Web : Mobile Widget Platforms
  •  Programming the Mobile Web : Geolocation and Maps - Showing a Map
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 2)
  •  Mobile Application Security - BlackBerry Security - Permissions and User Controls (part 1) - RIM Controlled APIs
  •  Windows Phone 7 Development : Working with Controls and Themes - Introducing the Metro Design System
  •  Windows Phone 7 Development : WebBrowser Control - Saving Web Pages Locally
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 3)
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 2) - Google Gears
  •  Programming the Mobile Web : Geolocation and Maps - Detecting the Location (part 1) - W3C Geolocation API
  •  
    Top 10
    Windows Server 2003 : Domain Name System - Command-Line Utilities
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 2)
    Microsoft .NET : Design Principles and Patterns - From Principles to Patterns (part 1)
    Brother MFC-J4510DW - An Innovative All-In-One A3 Printer
    Computer Planet I7 Extreme Gaming PC
    All We Need To Know About Green Computing (Part 4)
    All We Need To Know About Green Computing (Part 3)
    All We Need To Know About Green Computing (Part 2)
    All We Need To Know About Green Computing (Part 1)
    Master Black-White Copying
    Most View
    Collaborating Within an Exchange Server Environment Using Microsoft Office SharePoint Server 2007 : Exploring Basic MOSS Features
    Get A Faster, Safer PC (Part 3) - Make text easier to read, Disable a laptop touchpad
    Algorithms for Compiler Design: THE MACHINE MODEL
    iPhone, iPad touch and iPad : Realikety
    Storage, Screens And Sounds (Part 1)
    Audio Cleaning Lab MX - makes some sounds sound better
    SQL Server 2008 : Using Remote Stored Procedures
    Sony Xperia Go
    Web Security Testing : Manipulating Sessions - Analyzing Session Randomness with WebScarab
    Understanding the Basics of Collaboration in SharePoint 2010 : Microsoft Office Integration
    Buyer’s Guide - Keyboard and mice (Part 2) - Gigabyte Multimedia Ultra-slim Profile Keyboard GK-K6150, Microsoft Natural Ergonomic Keyboard 40000
    How To Store Your Files In The Ether
    Expert advice: Printer & Scanner (Part 2) - Samsung ML-2955DW
    SharePoint 2010 : PerformancePoint Services (part 2) - Using PerformancePoint
    SQL Server System and Database Administration : System Databases
    Is The Personal Blog Dead? (Part 2) - Going Mainstream
    Choosing The Right Gear For The Right Job
    Panasonic KX-MB1530CX Multi-Function Printer : A small workhorse for document printing
    Master Black-White Copying
    Windows 7 : Installing and Running Your Software (part 1)