
By Adam Grocholski | Article Rating: |
|
April 23, 2013 03:00 PM EDT | Reads: |
1,007 |

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”>
</Grid>
Add a <TextBlock> element to the grid:
<Grid x:Name=”LayoutRoot” Background=”Transparent”>
<TextBlock x:Name=”messageFromUser” />
</Grid>
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”
Orientation=”Vertical”>
<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}}”
Click=”sendButton_Click”/>
</StackPanel>
</Grid>
ANDROID HINT |
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:
private
void sendButton_Click(object sender, RoutedEventArgs e)
{
}
VISUAL STUDIO TIP |
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.
- Get the text the user entered.
- 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.
- 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),
UriKind.Relative);
//Perform the navigation
this.NavigationService.Navigate(uri);
}
ANDROID HINT |
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); startActivity(intent); } |
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
protected
override
void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(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)
{
base.OnNavigatedTo(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 =
this.NavigationContext.QueryString["message"];
}
Run the app (F5) to see the results!
Conclusion
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
- Getting started
- Developing apps
- Testing apps
- Monetizing apps
- Concepts and architecture
- Windows Phone API reference
Read the original blog entry...
Published April 23, 2013 Reads 1,007
Copyright © 2013 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Adam Grocholski
Hey there! My name is Adam Grocholski, and I'm a Technical Evangelist at Microsoft where I spend time focusing on Windows, Windows Phone, and Windows Azure. I live in the frozen tundra of Minnesota and run thinkfirstcodelater.com. You can also follow me on twitter at @codel8r.
![]() Apr. 22, 2018 04:00 PM EDT Reads: 6,896 |
By Elizabeth White ![]() Apr. 22, 2018 03:00 PM EDT Reads: 13,119 |
By Pat Romanski Apr. 22, 2018 02:45 PM EDT Reads: 2,337 |
By Pat Romanski Apr. 22, 2018 02:30 PM EDT Reads: 1,375 |
By Liz McMillan Apr. 22, 2018 02:00 PM EDT Reads: 1,696 |
By Yeshim Deniz ![]() Apr. 22, 2018 12:45 PM EDT Reads: 1,990 |
By Elizabeth White Apr. 22, 2018 12:15 PM EDT Reads: 1,751 |
By Pat Romanski ![]() Apr. 22, 2018 12:00 PM EDT Reads: 5,431 |
By Elizabeth White ![]() Apr. 22, 2018 12:00 PM EDT Reads: 3,961 |
By Elizabeth White ![]() Apr. 22, 2018 10:45 AM EDT Reads: 6,838 |
By Yeshim Deniz Apr. 22, 2018 10:00 AM EDT Reads: 999 |
By Pat Romanski ![]() Apr. 22, 2018 09:45 AM EDT Reads: 8,659 |
By Pat Romanski Apr. 22, 2018 09:15 AM EDT Reads: 2,271 |
By Liz McMillan Apr. 22, 2018 09:00 AM EDT Reads: 2,621 |
By Elizabeth White ![]() Apr. 22, 2018 09:00 AM EDT Reads: 6,445 |
By Yeshim Deniz Apr. 22, 2018 09:00 AM EDT Reads: 2,724 |
By Yeshim Deniz Apr. 22, 2018 08:45 AM EDT Reads: 3,138 |
By Pat Romanski ![]() Apr. 22, 2018 08:30 AM EDT Reads: 8,808 |
By Pat Romanski ![]() Apr. 22, 2018 05:15 AM EDT Reads: 3,786 |
By Elizabeth White ![]() Apr. 22, 2018 04:00 AM EDT Reads: 17,314 |