agDOTNETKICKS - Rudi Grobler

agDOTNETKICKS

I have to get my feet wet using Silverlight 2.0. What better way than creating a simple application? I decided to create a very simple dotnetkicks reader!

First things first:

1) Create a new silverlight 2.0 project using VS2008 (Blend 2.5 should also work)

2) Add a ItemsControl to the main page

<ItemsControl x:Name="ArticlesItemsControl" />

NOTE: I added a ItemsControl here because I don't need the selecting provided by the ListBox

3) Create a very basic RSS-like data structure to bind too for each article posted on DotNetKicks

public class Article
{
    public string Title { get; set; }
    public string Description { get; set; }
    public Uri Link { get; set; }
    public Uri KickUri { get; set; }
    public int Kicks { get; set; }
}

4) Asynchronously fetch the RSS feed from the DotNetKicks site

List<Article> _articles = new List<Article>();
const string feedUrl = @"http://feeds.feedburner.com/dotnetkicks?format=xml";

private void GetArticles(string url)
{
    WebClient wc = new WebClient();
    wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(articles_DownloadStringCompleted);
    wc.DownloadStringAsync(new Uri(url));
}

5) Parse the retrieved XML data

void articles_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    XDocument doc = XDocument.Parse(e.Result);

    foreach (XElement article in doc.Descendants("item"))
    {
        Article a = new Article();
           
        a.Title = (string)article.Element("title");
        // The rest of the parsing is removed to reduce complexity

        _articles.Add(a);
    }
    ArticlesItemsControl.ItemsSource = _articles;
}

Once the RSS feed has been parsed and all the articles are added to the List, set the ItemsSource on the ItemsControl!

6) All that is now left to do is to replace the ItemTemplate with our own custom DataTemplate

<ItemsControl x:Name="ArticlesItemsControl">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <StackPanel Orientation="Horizontal" Margin="10,0,10,10">
                <StackPanel>
                    <Border Background="Aqua" Width="50" Height="50" CornerRadius="5,0,5,0">
                        <StackPanel>
                            <TextBlock Text="0" FontSize="22" HorizontalAlignment="Center"/>
                            <TextBlock Text="Kicks" FontSize="10" HorizontalAlignment="Center" Foreground="#80000000"/>
                        </StackPanel>
                    </Border>
                    <Border Background="White" Width="50" Height="20" Margin="0,5,0,0" BorderBrush="Black" BorderThickness="1">
                        <HyperlinkButton Content="Kick It" NavigateUri="{Binding KickUri}" HorizontalAlignment="Center" VerticalAlignment="Center" />
                    </Border>
                </StackPanel>
                <StackPanel Margin="10,0,0,10">
                    <HyperlinkButton Content="{Binding Title}" NavigateUri="{Binding Link}" FontFamily="Trebuchet MS" FontSize="12" />
                    <TextBlock Text="{Binding Description}" TextWrapping="Wrap" FontFamily="Arial" FontSize="8.5" Height="Auto" />
                </StackPanel>
            </StackPanel>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

And here is the final result:

I am busy updating the example to use the JSON service provided... this will allow me to get a richer set of data back... As soon as I have it working, I will post it!

UPDATE: The app is now hosted using Silverlight Streaming, have a look here

kick it on DotNetKicks.com
Published Tuesday, March 18, 2008 7:55 AM by rudi

Comments

# agDotNetKicks

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Tuesday, March 18, 2008 7:57 AM by DotNetKicks.com

# re: agDOTNETKICKS

Where's the live demo?

Tuesday, March 18, 2008 12:39 PM by Will

# re: agDOTNETKICKS

I will post a live demo as soon as I have the JSON stuff working...

Rudi Grobler

http://dotnet.org.za/rudi

Tuesday, March 18, 2008 4:56 PM by rudi

# re: agDOTNETKICKS

I thoroughly enjoy reading your articles - both here and on CodeProject - and this is another good one. Looking forward to you doing a WPF or Silverlight presentation *hint hint*

Wednesday, March 19, 2008 6:36 AM by William

# Hosting agDotNetKicks using Silverlight Streaming

I had some request to host agDotNetKicks . I found Tim Sneath &#39;s article about hosting Silverlight

Wednesday, March 19, 2008 7:15 AM by Rudi Grobler

# re: agDOTNETKICKS

Tnx William...

I am working on some ideas for WPF presentations... I just don't want to do the normal overview of WPf or a snippet here, snippet their demo! I would love to try and do something real-world... just haven't figured out what yet!

Wednesday, March 19, 2008 7:19 AM by rudi

# re: agDOTNETKICKS

Ok,

the app is now hosted using Silverlight Streaming... have a look here:

dotnet.org.za/.../hosting-agdotnetkicks-using-silverlight-streaming.aspx

Regards,

Rudi

Wednesday, March 19, 2008 8:36 AM by rudi

# W&ouml;chentliche Rundablage: Silverlight 2, .NET, LINQ, ASP.NET MVC, Design | Code-Inside Blog

Pingback from  W&ouml;chentliche Rundablage: Silverlight 2, .NET, LINQ, ASP.NET MVC, Design | Code-Inside Blog