Support us on YouTube by Subscribing our YouTube Channel. Click here to Subscribe our YouTube Channel

Thursday 4 June 2015

Page Navigation in Windows Phone 8 Application

In this article, we will learn how to navigate from one page to another as well as how to pass parameter or value from one page to another. In the previous tutorials, we saw:


Page navigation is used to navigate from one page to another in Windows Phone 8. Navigation is managed by NavigationService Class. NavigationService provides Navigate method which navigates to the content specified by uniform resource identifier i.e. URI. Navigate method takes Uri, which is the name of the page on which you want to navigate.

Let's Begin:
1. Navigation from one page to another.
Create a New Windows Phone Application and select Windows Phone 8.0 as a target Windows Phone OS for this application and click on OK. Drop a button control on MainPage.xaml as shown in below image and named it as btn_navigationPage.
MainPage.xaml Code: 
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <Button Name="btn_navigationPage"
                    Content="Navigate between page"
                    VerticalAlignment="Top">
            </Button>
</Grid>
Now, Right Click on Project -> Add -> Click on New Item.
Add a Page (named it as AboutPage.xaml).
Add a HyperlinkButton control(named it as hlb_GoBack). Design the UI of page as shown in below image.
AboutPage.xaml Code: 
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <StackPanel>
            <TextBlock Text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. In lectus nisl, tempor sit amet sollicitudin ut, finibus ullamcorper nisl. Nulla facilisi." TextWrapping="Wrap"></TextBlock>
            <HyperlinkButton Name="hlb_GoBack" Content="Go Back"/>
            </StackPanel>
</Grid>
Now, go to MainPage.xaml and double click btn_navigationPage Button to create a click event and add the following code:
private void btn_navigationPage_Click(object sender, RoutedEventArgs e)
{
        NavigationService.Navigate(new Uri("/AboutPage.xaml",UriKind.Relative));
}
In preceding code, we have called Navigate method which navigates to the content specified by uniform resource identifier. We set UriKind.Relative because AboutPage.xaml is part of the same project or XAP file. We have already added HyperlinkButton on AboutPage.xaml. We will use this for navigating back to the previous page (We don't have to code for hardware back key button in Windows phone for navigating back to previous page). Double click on it to generate its click event and add the following lines of code.
private void hlb_GoBack_Click(object sender, RoutedEventArgs e)
{
        //Navigate to most recent entry in the back navigation history
        NavigationService.GoBack();
}
NavigationService.GoBack() method redirects the user to the previous page.
Preview:

2. Pass parameter/value from one page to another:
Let's create a New Project and add a Page(named it as FillDetails.xaml). Drop TextBox (named as txt_Name) and Button(named as btn_Submit) control on it.
FillDetails.xaml Code: 
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
      <StackPanel>
            <TextBlock Text="Enter your Name:"></TextBlock>
            <TextBox Name="txt_Name"></TextBox>
            <Button Name="btn_Submit" Content="Submit"></Button>
      </StackPanel>
</Grid>
Add another page(named as DisplayDetails.xaml) on which we will display the data passed from FillDetails.xaml page.
Now, go to FillDetails.xaml page and create click event of btn_Submit by double clicking on it.
private void btn_Submit_Click(object sender, RoutedEventArgs e)
{
        // Checking txt_Name is not empty
        if (txt_Name.Text != "")
        {
           NavigationService.Navigate(new Uri("/DisplayDetails.xaml?name=" + txt_Name.Text, UriKind.Relative));
        }
        else
        {
           MessageBox.Show("Please Provide Name!");
        }
}
In preceding code, we passed name as query string parameter in Uri. Open DisplayDetails.xaml.cs code and override OnNavigatedTo method. OnNavigatedTo method is called when page becomes the active page in a frame. Get the value from the query string and display it on lbl_display using the Text property.
protected override void OnNavigatedTo(NavigationEventArgs e)
{
     base.OnNavigatedTo(e);
     string name = "";
     if(NavigationContext.QueryString.TryGetValue("name", out name))
     {
          lbl_display.Text = "Hello! " + name+"\n Welcome to Windows Phone 8 Apps Development Tutorial Series.";
     }
}
Preview: 
I hope you like it. Thanks.
[Download Source Code via Google Drive]

0 comments:

Post a Comment

Subscribe us on YouTube

Subscribe Now

Popular Posts

Contact us

Name

Email *

Message *

Like us on Facebook