Monday, March 29, 2010

My First Windows Phone 7 Application

This is a walkthrough on creating a sample RSS reader for Windows Phone 7 (WP7), make sure to check my previous post for some useful links on how to start developing for WP7

  • create a new Windows Phone List Application: this project template provides you with a sample application that contains two pages, the main page for displaying a list of items, when you click on one of the list items a nice transition takes you to the second page which displays the list items details

1

  • we will use this sample list application as starting point for creating our RSS reader, we will have 3 pages, the first will have a list of RSS feeds, when we click on each feed we will go to the second page that contains a list of the items available in this feed, and when we click on a particular item we move to the third page which displays the feed item details
  • let’s start by defining our data models, the Windows Phone List Application project template provides you with a sample view model classes in the ViewModels folder, it also provides a sample data in the SampleData/MainViewModelSampleData.xaml file, this data is used by the visual studio and blend designer so that you can see (at design time) how your controls will look like when it’s bound to data.
  • we will create three view model classes in the ViewModels folder:

RssReaderViewModel: represents the main view model, contains a list of Feeds

3

FeedViewModel: represents the feed view model, contains the feed name, feed URL and a list of feed items

4

FeedItemViewModel: represent the feed item details, contains title, description, URL and publishing date

5

  • we will create three xaml files to use as sample data

MainViewModelSampleData.xaml

6

FeedViewModelSampleData.xaml

7

FeedItemViewModelSampleData.xaml

8

  • Now our project structure will look like the following

2

  • the MainPage.xaml is the main page, it contains a ListBox that displays the feeds

9

in the page constructor the DataContext is set to a new instance of the RssReaderViewModel class, and as you can see in the xaml file the ListBox ItemsSource property is bound to the Feeds collection, when you click on an item in the list the selected item is captured and a nice transition begins (provided by default in the Windows Phone List Application project template), once the transition completes the application navigates to FeedViewModelSampleData.xaml and sets the DataContext to the selected item

10

  • the FeedDetailsPage.xaml page has a ListBox that is bound to the Items property of the FeedViewModel class 12

in the constructor the RSS feed is downloaded and the items in the RSS feed is used to populate the Items collection in the FeedViewModel class

13

when we click on the ListBox we capture the selected item and a nice transition animation begins, once completed we navigate to the FeedItemDetailsPage.xaml page and sets the DataContext to the selected item

14

  • the FeedItemDetailsPage.xaml page will display the feed item details (title, description, date published)

11

That’s pretty much everything in the application, You can download the source code from here

No comments: