DZone
Mobile Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > Mobile Zone > Android to Windows Phone 8 Part 6: Creating a Second Page

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

Adam Grocholski user avatar by
Adam Grocholski
·
Apr. 24, 13 · Mobile Zone · Interview
Like (0)
Save
Tweet
2.97K Views

Join the DZone community and get the full member experience.

Join For Free

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.

  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),

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



Windows Phone Android (robot)

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Progressive Web Apps vs Native Apps: Differences and Similarities
  • Top 7 Features in Jakarta EE 10 Release
  • When Writing Code Isn't Enough: Citizen Development and the Developer Experience
  • 12 Modern CSS Techniques For Older CSS Problems

Comments

Mobile Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • MVB Program
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends:

DZone.com is powered by 

AnswerHub logo