Print Article
Add to your CodeProject bookmarks
Comment on this article
Report this article as inappropriate
A Simple Silverlight RSS ReaderThis is a simple silverlight RSS Reader which pulls out data from a feed url.
IntroductionThis article shows how to create a simple RSS Reader in silverlight.This is a beginner article showing basic Templating, Binding and such basic concepts in silverlight. Screenshot
BackgroundSource code consist of a Silverlight project, a Web Project and a WCF service hosted in the web application. I developed this sample in Visual Studio 2008. Using the codeWe can start creating a silverlight application from the Visual studio templates and it'll automatically create a silverlight project and a Web application into which silverlight is hosted.We need to fetch data from the feed url that a user is entered and for that purpose, we are going to use a WCF service so that the silverlight client can make asynchronous calls and can fetch the response.So Lets start by adding a WCF service to the Web application, here in my sample its namespace RSSReader.Web
{
[ServiceContract]
public interface IRSSReaderService
{
[OperationContract]
IEnumerable
We need to implement the operation contract GetFeed(string uri) in our service code behind which is implementing [DataContract] public class RSSItem { [DataMember] public string Title { get; set; } After creating this DataContract we create an XmlReader with the specified url and load the SyndicationFeed items from this XMLReader. Now we can use LINQ for iterating through this syndication items and for fetching the required information from those items.We are populating the RSSItem that we created and sending an IEnumerable of this object from the service to the client.So our [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class RSSReaderService : IRSSReaderService
{
public IEnumerableSo our service is completed and ready for consumption by the client.Now we need to create the silverlight client.Add service reference to the Silverlight project and then create a user control <ListBox x:Name="RSSFeed"
VerticalAlignment="Stretch"
HorizontalAlignment="Stretch"
Grid.Row="2"
Grid.ColumnSpan="2">
<ListBox.ItemTemplate>
<DataTemplate>
<Grid MinWidth="{Binding ElementName=LayoutRoot, Path=ActualWidth}"
MaxWidth="{Binding ElementName=LayoutRoot, Path=ActualWidth}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}"
FontFamily="Verdana"
FontSize="13" />
<TextBlock Grid.Row="2"
Text="{Binding PublishDate}"
HorizontalAlignment="Left"
FontFamily="Verdana"
FontSize="11" />
<HyperlinkButton Grid.Row="2"
Content="Read Article>>"
NavigateUri="{Binding Permalink}"
HorizontalAlignment="Center"
FontFamily="Verdana"
FontSize="11"
FontStyle="Italic"
ToolTipService.ToolTip="{Binding Title}"
TargetName="__blank" />
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
We are not using MVVM pattern for this application as this is a very basic sample.We can follow the normal way of event subscribtion and all ie, straight forward and we have all the logic in the code behind itself.While clicking the fetch private void FetchRSS_Click(object sender, RoutedEventArgs e)
{
if (!string.IsNullOrEmpty(RSSFeedUrlTextBox.Text.Trim())
&& Uri.IsWellFormedUriString(RSSFeedUrlTextBox.Text.Trim(), UriKind.Absolute))
{
LoadingTextBlock.Visibility = Visibility.Visible;
RSSFeed.Items.Clear();
RSSReaderServiceClient RSSReaderServiceClient = new RSSReaderServiceClient();
RSSReaderServiceClient.GetFeedCompleted += new EventHandlerAdd this usercontrol to the main page and compile it.Before running the application don't forget to put the cross domain policy file in the web application root.Otherwise silverlight client can't communicate with the WCF service. If you get any run time errors for the sample application then delete and add the service reference again in the silverlight project. History
LicenseThis article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL) About the Author
Comments and Discussions
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+PgUp/PgDown to switch pages.
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||