Sunday, March 21, 2010

Building a Windows 7 Phone Weather Application Using SilverLight

This WP7 application takes Zip code as a input from user and calls the Web Service (.ASMX) using HTTP-GET. In this CTP SDK bits we can not add Service Reference to the  project so here we are going to use WebClient class to invoke the Web Service and get the Response as XML string  then using LINQ TO XML we are going to parse the XML . Here is the look and feel of the WP7 Weather application  once we finish building this.

To get weather information by Zip code i am using the following  XML Web Service.

http://www.webservicex.net/WeatherForecast.asmx its free.

image

Steps to Create this application.

  1. Create New Windows Phone 7 application project
  2. Build UI using XAML code.
  3. Get Input from user and call the .ASMX Web service (HTTP-GET)
  4. Get the Response and Parse it and create UDT ( User Defined Type which stores XML Data).
  5. Bind UDT to List BOX.

First Create New Windows Phone 7 application .

image

Here is the XAML code for the above Weather Apps GUI.

 image

Here i am using InputScope property of Text Box to show only numeric keyboard when user enter Zip code in Textbox. Here i am setting Inputsopename NameValue to PostalCode.

image

image

Create the UDT ( User Defined Type) class with the following properties to store XML Weather Data as a .NET Type. 

image

In Windows Phone 7 application project  right now we can not add Service Reference so we are going to use WebClient class to invoke the web service. This is Siliverlight application so we need to invoke the Web Service method as Asynchronous call .

image

Once we get the response from Weather WebSerice we are going to use LINQ TO XML to parse the XML data and create the WeatherData UDT type. [ NOTE :We need to add System.Linq.Xml Assembly as reference to our project to parse XML using LINQ]. Once we parse the XML we are setting result to Listbox Itemsource property.

image

image

Nandri(Thanks)

SreenivasaRagavan

1 comment:

Charlie said...

Very interesting article, I always have had in mind about learning to program applications for cell phones, specially iPhones. Silverlight, .Net and ASP are excellent platforms for software development, these have some similar working effects to the generic viagra in blood circulation.