Export (0) Print
Expand All
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

How to perform page navigation on Windows Phone 8

August 19, 2014

Applies to: Windows Phone 8 and Windows Phone Silverlight 8.1 | Windows Phone OS 7.1

 

This topic will show you how to navigate back and forth between different pages of content in your app.

This topic contains the following sections.

 

In this section, you will create an additional page of content that you can navigate to from your app main page.

To create an additional page

  1. Create a new project by selecting the File | New Project menu command.

  2. The New Project window will be displayed. Expand the Visual C# templates, and then select the Windows Phone templates.

  3. Select the Windows Phone App  template. Fill in the project name as desired.

  4. From the designer view on MainPage.xaml, select page title and change this title text to main page in the Text properties or in the XAML directly.

    AP_Con_mainpage
  5. Right-click your project name in the Solution Explorer, select Add from the menu, and then New Item.

  6. Choose Windows Phone Portrait Page, change the name to SecondPage, and select Add at the bottom of the page.

  7. From the designer view on SecondPage.xaml, select page title and change this title text to second page in the Text properties or in the XAML directly.

This section will show you how to navigate back and forth between your MainPage.xaml and SecondPage.xaml.

To navigate between pages

  1. On MainPage.xaml, drag a HyperlinkButton control from the Toolbox onto the designer surface. Select the control and change the Content property to Navigate to Second Page or do this directly in the XAML. You may need to expand the control width to see all of the text.

  2. Double-click the hyperlink button to add an event handler for the hyperlink click event. The MainPage.xaml.cs file will open.

  3. For the hyperlinkButton1_Click event handler, add the following code:

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

    You can also accomplish the above in the MainPage.xaml by setting the NavigateUri property for the hyperlink control to the second page. For example: NavigateUri = “/SecondPage.xaml”

  4. On SecondPage.xaml, drag a Button control onto the designer surface. Select the control and change the Content property to Navigate Back to Main Page or do this directly in the XAML. You may need to expand the control width to see all of the text.

  5. Double-click the button to add an event handler for the button click event. The SecondPage.xaml.cs file will open.

  6. For the button1_Click event handler, add the following code:

    
      private void button1_Click(object sender, RoutedEventArgs e)
            {
                NavigationService.GoBack();
            }
    
  7. Run the app by selecting the Debug | Start Debugging menu command. This will open the emulator window and launch the app.

When you run the app, you will see that it consists of two pages: the main page and a second page. You can navigate from the main page to the second page using a hyperlink with the destination URI configured in its event handler. You can return from the second page to the main page by using the GoBack() method of the navigation service.

NoteNote:

Although the GoBack() method was used in this example, the hardware Back button would also have the effect of returning to the previous page.

AP_Con_mainnavAP_Con_secnav

In this section, you will take text from one page and add the text to a text block control on another page. You will use the previous project for the following procedures.

To pass string data from page to page

  1. On MainPage.xaml, drag a TextBox control and a Button control from the Toolbox onto the designer surface and place the controls side by side. Clear the text already present in the TextBox control in the control properties. For the Button control, rename the control to passParam and change the button text to Go in either the control properties or in the XAML directly.

  2. Double-click the Go button to add an event handler for the button click event. The MainPage.xaml.cs file will open.

  3. For the passParam_Click event handler, add the following code:

    
        private void passParam_Click(object sender, RoutedEventArgs e)
            {
                
                NavigationService.Navigate(new Uri("/SecondPage.xaml?msg=" + textBox1.Text, UriKind.Relative));
            }
    
    NoteNote:

    This line of code will be used to navigate to the second page content and pass the string data entered in the newly created TextBox.

  4. On SecondPage.xaml, drag a TextBlock control from the Toolbox onto the designer surface. Clear the text already present in the TextBlock control in the control properties. You may need to expand the height and width of the control and choose a larger font size for easier viewing. Also name the TextBlock you have created to textBlock1 for this example.

  5. On SecondPage.xaml.cs, create the following method:

       protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
            {
                base.OnNavigatedTo(e);
    
                string msg = "";
                
                if (NavigationContext.QueryString.TryGetValue("msg", out msg))
    
                    textBlock1.Text = msg;
    
               
            }
    
  6. Run the app by selecting the Debug | Start Debugging menu command. This will open the emulator window and launch the app.

    On the main page, enter some text into the TextBox control and tap Go. When you reach the second page, your typed text should be visible in the TextBlock control on the second page.

    AP_Con_mainpasstxtAP_Con_secondpasstxt

Show:
© 2014 Microsoft