Wednesday 22 October 2008

A Simple RSS Reader in WPF

I present here, a simple Rss reader written almost entirely in XAML. I personally find WPF Data binding powerful and exciting to work with.

To start with, i've used the XmlDataProvider class to hook up the source to an rss feed and set it's XPath property to "rss/channel/item", so that i get all items in the channel.

The layout is an utterly simple dockpanel which has a stackpanel docked to the top, a status bar docked to the bottom and a grid which fills up the remaining space.

The databinding is self explanatory with the following code.,However an important thing to note is that the <Textbox> which has the rss feed link is binded to the source directly. The 'UpdateSourceTrigger' property is by default set to LostFocus(). I've changed it to 'PropertyChanged', so that when a user enters an rss feed, the bindings get updated. Also notice that 'BindsDirectlyToSource' is set to True. Another interesting feature is the Master-Detail binding. Notice that between Line numbers 53-63. The listbox's selection is binded to  description and link which are textblocks inside a stack panel. Now, when the listbox gets focus, we bind the frame's source to the uri in the texbox.

I've added some styles to the listbox aswell. all of which could be found in the source code. This surely is very simple and yet a foundation to write a powerful rss reader in wpf. :)

We could add more rss feeds, write value converters, validation rules etc. as required.


Window1.xaml

   1: <Window x:Class="RssReader.Window1"


   2:     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"


   3:     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"


   4:     Title="RssReader" Height="600" Width="800">


   5:     


   6:     <Window.Resources>


   7:     <XmlDataProvider x:Key="rssdata"


   8:                      Source="http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml"


   9:                      XPath="rss/channel/item"/>


  10:         


  11:     </Window.Resources>


  12:     


  13:     <DockPanel   DataContext="{Binding Source={StaticResource rssdata}}">


  14:         <StackPanel DockPanel.Dock="Top"


  15:                     TextElement.FontWeight="Bold" Background="Gray">


  16:         


  17:             <TextBlock Text="{Binding XPath=./../title}"


  18:                     FontSize="20" Margin="10 10 10 0"/>


  19:             


  20:             <TextBlock Text="{Binding XPath=./../description}"


  21:                     FontSize="10" FontWeight="Normal" Margin="10 0"/>


  22:             


  23:             <TextBox Margin="5" Text="{Binding Source={StaticResource rssdata},                             


  24:                                 BindsDirectlyToSource=True,


  25:                                 Path=Source,


  26:                                 UpdateSourceTrigger=PropertyChanged}"/>


  27:         </StackPanel>


  28:  


  29:         <StatusBar DockPanel.Dock="Bottom">


  30:             <StatusBarItem Content="{Binding XPath=title}"/>


  31:             <Separator/>


  32:             <StatusBarItem Content="{Binding XPath=pubDate}"/>


  33:         </StatusBar>


  34:  


  35:         <Grid>


  36:             <Grid.ColumnDefinitions>


  37:                 <ColumnDefinition Width="25*"/>


  38:                 <ColumnDefinition Width="75*"/>


  39:                 <ColumnDefinition/>


  40:             </Grid.ColumnDefinitions>


  41:            


  42:             <ListBox Grid.Column="0" IsSynchronizedWithCurrentItem="True"                     


  43:                      ItemsSource="{Binding}" DisplayMemberPath="title"


  44:                      Style="{StaticResource ListBoxHand}"/>


  45:             <GridSplitter/>


  46:             


  47:             <Grid Grid.Column="1">


  48:                 <Grid.RowDefinitions>


  49:                     <RowDefinition Height="Auto"> </RowDefinition>


  50:                     <RowDefinition Height="85*"> </RowDefinition>


  51:                 </Grid.RowDefinitions>


  52:  


  53:                 <ListBox x:Name="selection" Grid.Row="0" IsSynchronizedWithCurrentItem="True"                         


  54:                          Style="{StaticResource SimpleListBox}"


  55:                          VerticalAlignment="Stretch"


  56:                          GotFocus="selection_GotFocus">


  57:                              


  58:                     <StackPanel>


  59:                             <TextBlock Text="{Binding XPath=description}" />


  60:                             <TextBlock x:Name="txtlink" Text="{Binding XPath=link}"/>


  61:                         </StackPanel>


  62:                 </ListBox>


  63:                     <Frame x:Name="Explorer" Grid.Row="1"/>


  64:             </Grid>


  65:         </Grid>


  66:  


  67:       </DockPanel>


  68:     </Window>


  69:  




Window1.xaml.cs





   1: private void selection_GotFocus(object sender, RoutedEventArgs e) {


   2:   Uri uri = new Uri(this.txtlink.Text.ToString());


   3:   this.Explorer.Source = uri;


   4: }


   5:   




Screenie



rssreader_wpf


C0d3 : here

No comments:

Post a Comment