Android to Windows Phone 8 Part 6: Creating a Second Page

DZone 's Guide to

Android to Windows Phone 8 Part 6: Creating a Second Page

· Mobile Zone ·
Free Resource

Over the past several weeks I’ve been working on some content I’m excited to finally share with you through a series of blog posts. This series will introduce you to Windows Phone 8 development from an Android developer’s perspective. Through the course of the series you’ll create your first app. It won’t be anything pretty, but you’ll learn the ins and outs of the development environment, how to create a simple user interface, and how to perform navigation. Along the way you’ll see some Android Hints that will help make it easier for you to transition your existing skills to the Windows Phone platform. You’ll also see some Visual Studio Tips to make you more productive in your development environment. Good luck!

In the previous lesson you created a simple UI that accepts text from a user. In this lesson you’ll create a second page in your application to display the user’s input.

Create the Second Page

Right-click the project in Solution Explorer, click Add, and click New Item.

In the Add New Item dialog, select Windows Phone under Visual C#.

Select the Windows Phone Portrait Page template, give the page a name of SecondPage.xaml, and click Add.

You should now see SecondPage.xaml in Solution Explorer.

If SecondPage.xaml is not open, double-click it.

Remove the content from the outermost <Grid> element. You should end up with something that looks like this:

<!–LayoutRoot is the root grid where all page content is placed–>

<Grid x:Name=”LayoutRoot” Background=”Transparent”>



Add a <TextBlock> element to the grid:

<Grid x:Name=”LayoutRoot” Background=”Transparent”>

<TextBlock x:Name=”messageFromUser” />



Respond to the Send Button

You now need to get the message from the first page to the second page. Start by opening up MaingPage.xaml.

To respond to the <Button> element’s Click attribute to the element

 <Grid x:Name=”LayoutRoot” Background=”Transparent”>

<StackPanel x:Name=”theStackPanel”


<TextBlock x:Name=”theLabel”

Text=”{Binding Path=LocalizedResources.MessageLabelText,

Source={StaticResource LocalizedStrings}}” />

<TextBox x:Name=”theMessage” />

<Button x:Name=”sendButton”

Content=”{Binding Path=LocalizedResources.SendButtonText,

Source={StaticResource LocalizedStrings}}”





The Click attribute is similar to the android:onClick attribute of the Android <Button> element.


Open the code-behind file for MainPage.xaml (MainPage.xaml.cs) and add the following method:

void sendButton_Click(object sender, RoutedEventArgs e)





You can open a code-behind file for a file you’re working with in design view by pressing F7.


Now you need to do three things.

  1. Get the text the user entered.
  2. Create a URI to navigate to SecondPage.xaml that includes the text entered by the user. This is done by including the text as a query string parameter.
  3. Navigate to SecondPage.xaml.

All three can be accomplished by adding the following to the sendButton_Click method:

 private void sendButton_Click(object sender, RoutedEventArgs e)


//Get the message from the <TextBox> element

var message = this.theMessage.Text;


//Use a default message if none is provide

if (string.IsNullOrEmpty(message))

message = “Hello World!”;


//Create the uri to navigate to SecondPage.xaml, passing

//the message through in the query string

var uri = new Uri(string.Format(“/SecondPage.xaml?message={0}”, message),



//Perform the navigation





In Android you would create an Intent and call the startActivity method to navigate to the second page with code similar to the following:


public void sendButton_Click(View view) {

Intent intent = new Intent(this, SecondPageActivity.class);




If you run your app now (F5) and press the button, you’ll notice that you go to the second page. We’ll work on displaying the message in the last part of this lesson.

Display the Message

Now that you’re able to navigate to SecondPage.xaml from MainPage.xaml you need to display the message the user entered.

Open SecondPage.xaml.cs (the code-behind file for SecondPage.xaml).

Add an override for the OnNavigatedTo method

void OnNavigatedTo(NavigationEventArgs e)





Finally retrieve the query string parameter from the page’s NavigationContext property and display in the <TextBlock> element with the following code:

 protected override void OnNavigatedTo(NavigationEventArgs e)




//Check that the query string contains the message parameter

//and display on the page if found

if (this.NavigationContext.QueryString.ContainsKey(“message”))

this.messageFromUser.Text =



Run the app (F5) to see the results!


Congratulations! You just build your first Windows Phone application. To learn more about building Windows Store apps, continue to follow this series. The next installment is Working with Data.

Previous Posts in this Series

  • Setting up the Development Environment
  • Creating Your First Windows Phone Project
  • Exploring the Windows Phone Project
  • Running the Windows Phone Application
  • Building a Simple User Interface

Additional Resources


Published at DZone with permission of Adam Grocholski , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}