Over a million developers have joined DZone.

Build an RSS Reader for Windows Phone 7

· Mobile Zone

Learn how to Deliver Better Mobile Apps Faster with Continuous Quality by managing the complexities of testing multiple devices and scenarios with this whitepaper from Perfecto Mobile.

In this lesson I will create a simple RSS(Really Simple Syndication) Reader for Windows Phone 7. I will use Linq-To-XML for reading a RSS feed. User interface will contain a Textbox to enter an URL of a RSS Feed and a ListBox element to display results.

Source code

Additional information

1. Creating a new project

First of all you need to create a new project. To do so open Visual Studio 2010 -> File -> New Project -> select Windows Phone Application there as it is shown on picture bellow.

eugenedotnet creating windows phone 7 project

2. Adding a new reference to solution

Next you need to add a reference to Microsoft.Xml.Linq library. You need to right click on the References directory under your project and select Add Reference there. Under .NET tab you will find Microsoft.Xml.Linq, push OK. Check screenshot bellow.

eugenedotnet add Microsoft.Xml.Linq reference

3. Modifying header

This step can be optional, but I have decided to add it to the lesson. In this step we simply modify TitleGrid element to specify the title of application.

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="24,24,0,12">
    <TextBlock x:Name="ApplicationTitle" Text="EUGENEDOTNET.COM" Margin="0,0,0,0" Style="{StaticResource PhoneTextNormalStyle}"/>
    <TextBlock x:Name="PageTitle" Text="RSS Reader" Margin="-3,-8,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
</StackPanel>
 

4. Modifying ContentGrid

ContentGrid will contain all the required UI elements to operate with our application. Basically, there will be three of them:

  • TextBox for user to enter an URL of a RSS Feed
  • Button to trigger an event to read feed
  • ListBox to display results using Data Binding feature of Silverlight (e.g {Binding Title} ). ListBox will display a title and a link to each of results.

NB! Pay attention that click event is added to a button in XAML. Also notice the new InputScope feature of Windows Phone 7 is applied to TextBox making it easier for user to enter URLs into Textbox element by providing a URL-specific virtual keyboard (SIP) layout by default.

<Grid x:Name="ContentGrid" Grid.Row="1">
    <Grid.RowDefinitions>
        <RowDefinition Height="70" />
        <RowDefinition Height="Auto" />
    </Grid.RowDefinitions>
    <TextBox Name="tbRSSFeedURL" Grid.Row="0" Text=""  Width="350" Margin="10,0,10,0" HorizontalAlignment="Left">
        <TextBox.InputScope>
            <InputScope>
                <InputScopeName NameValue="Url" />
            </InputScope>
        </TextBox.InputScope>
    </TextBox>
    <Button Name="btnReadFeed" Grid.Row="0" Content="Read" Width="110" Margin="10,0,10,0" HorizontalAlignment="Right" Click="btnReadFeed_Click" />
    <ScrollViewer Width="440" Height="530" Grid.Row="1" Margin="10, 20, 0, 0">
        <ListBox Name="listboxRSSFeedItems" Width="440" Height="530">
            <ListBox.ItemTemplate>
                <DataTemplate>
                    <Grid>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="Auto" />
                            <RowDefinition Height="Auto" />
                        </Grid.RowDefinitions>
                        <TextBlock TextWrapping="Wrap" Text="{Binding Title}" Grid.Row="0" FontSize="24" HorizontalAlignment="Left" />
                        <HyperlinkButton Content="Link to details" NavigateUri="{Binding Link}" HorizontalAlignment="Left" Grid.Row="1"  Margin="0,0,0,30" />
                    </Grid>
                </DataTemplate>
            </ListBox.ItemTemplate>
        </ListBox>
    </ScrollViewer>
</Grid>

5. Creating a class for a single RSS Feed item

Now we simply need to create a new container-class for holding RSS Feed data.

public class RSSFeedItem
{
    public string Title { get; set; }
    public Uri URL { get; set; }
}

6. Implementing event functionality

Next we need to write a code for processing “Read” button click event. It should create a new URI object and pass it to ReadRss method to begin reading a RSS Feed.

private void btnReadFeed_Click(object sender, RoutedEventArgs e)
{
    if (!string.IsNullOrEmpty(tbRSSFeedURL.Text))
    {
        ReadRss(new Uri(tbRSSFeedURL.Text, UriKind.Absolute));
    }
}

7. Implementing application logic

First of all make sure that MainPage.xaml class (codebehind) is using the following references.

using System.Xml.Linq; // for reading RSS using Linq-To-XML
using System.IO; // for Stream object
Next you need to implement a ReadRss method that takes URI (URL) as an input parameter. If request is successful then application will process results of reading a RSS feed (but only 10 first according to .Take(10) line in code) and add them using ForEach to a listbox. ListBox will display results according to the Data Binding specified above.
public void ReadRss(Uri rssUri)
{
    WebClient wclient = new WebClient();
 
    wclient.OpenReadCompleted += (sender, e) =>
    {
        if (e.Error != null)
            return;
 
        Stream str = e.Result;
        XDocument xdoc = XDocument.Load(str);
 
        // take 10 first results
        List<RSSFeedItem> rssFeedItems = (from item in xdoc.Descendants("item")
                                            select new RSSFeedItem()
                                            {
                                                Title = item.Element("title").Value,
                                                URL = new Uri(item.Element("link").Value, UriKind.Absolute),
                                            }).Take(10).ToList();
        // close
        str.Close();
 
        // add results to listbox
        listboxRSSFeedItems.Items.Clear();
        rssFeedItems.ForEach(item => listboxRSSFeedItems.Items.Add(item));
    };
    wclient.OpenReadAsync(rssUri);
}


Source: http://www.eugenedotnet.com/2010/05/w08-rss-reader-for-windows-phone-7/


Do you know Why Apps Succeed? Perfecto Mobile analyzed over 1,000 responses to their Digial Quality Strategies survey and aim to answer the question, "Why do apps succeed?" in this exclusive report.

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

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

{{ parent.tldr }}

{{ parent.urlSource.name }}