Tuesday, February 5, 2013

Windows Phone RSS FEED example

make a project with name  Recipe1_RssReader and have the following code to MainPage.xaml


<phone:PhoneApplicationPage 
    x:Class="Recipe1_RssReader.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"
    xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignWidth="480" d:DesignHeight="768"
    FontFamily="{StaticResource PhoneFontFamilyNormal}"
    FontSize="{StaticResource PhoneFontSizeNormal}"
    Foreground="{StaticResource PhoneForegroundBrush}"
    SupportedOrientations="Portrait" Orientation="Portrait"
    shell:SystemTray.IsVisible="True">

    <!--LayoutRoot is the root grid where all page content is placed-->
    <Grid x:Name="LayoutRoot" Background="Transparent">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <!--TitlePanel contains the name of the application and page title-->
        <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">
            <TextBlock x:Name="ApplicationTitle" Text="CH6-Recipes" Style="{StaticResource PhoneTextNormalStyle}"/>
            <TextBlock x:Name="PageTitle" Text="RSS Feeds" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>
        </StackPanel>
       

        <!--ContentPanel - place additional content here-->
        <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
            <TextBox Height="72" HorizontalAlignment="Left" Margin="0,52,0,0" Name="textBox1" Text="http://timesofindia.indiatimes.com/rssfeedsdefault.cms" VerticalAlignment="Top" Width="460" />
            <TextBlock Height="30" HorizontalAlignment="Left" Margin="14,32,0,0" Name="textBlock1" Text="RSS Feed Link:" VerticalAlignment="Top" />
            <Button Content="Go" Height="72" HorizontalAlignment="Left" Margin="0,116,0,0" Name="button1" VerticalAlignment="Top" Width="160" Click="button1_Click" />
        </Grid>
        <Grid x:Name="ContentPanel2" Margin="12,217,12,-13" Grid.Row="1">
            <Grid.Background>
                <SolidColorBrush />
            </Grid.Background>
            <ListBox x:Name ="lstItems" Margin="0,6,0,0" Background="#00FFFF02">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <Grid>
                            <Grid.RowDefinitions>
                                <RowDefinition />
                                <RowDefinition />
                                <RowDefinition />
                            </Grid.RowDefinitions>
                            
                            <StackPanel VerticalAlignment="Top">
                                <TextBlock Grid.Row="0" Text="{Binding Title}" FontWeight="Bold" Foreground="#FF266E00"/>
                                <TextBlock Grid.Row="1"  Text="{Binding Content}" Foreground="#FF31CACE" />
                                <HyperlinkButton TargetName="_blank" Grid.Row="2"  Name="DetailLink" Content="Details..." NavigateUri="{Binding URL}" Foreground="#FFB300FF" HorizontalAlignment="Left"/>
                                
                            </StackPanel>
                        </Grid>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>
        </Grid>


    </Grid>

</phone:PhoneApplicationPage>


the above given layout will be shown as a user interface while the MainPage.xaml.cs code is given below


using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;

// reference System.Xml.Linq
using System.Xml.Linq;
namespace Recipe1_RssReader
{
    public partial class MainPage : PhoneApplicationPage
    {
        // Constructor
        WebClient remoteXml;
        public MainPage()
        {
            InitializeComponent();

            remoteXml = new WebClient();
            remoteXml.DownloadStringCompleted += new DownloadStringCompletedEventHandler(remoteXml_DownloadStringCompleted);
        }

        //private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        //{
        //    if (e.Error != null)
        //        return;
        //    XDocument xdoc = XDocument.Parse(e.Result);
        //    IEnumerable<RssFeed> feeds;
        //   feeds = from task in xdoc.Descendants("item")
        //            select new RssFeed
        //            {
        //                Title = task.Attribute("title").Value,
        //                //Content = task.Attribute("description").Value,
        //                DatePosted = DateTime.Parse(task.Attribute("pubDate").Value),
        //                URI = task.Attribute("link").Value
        //            };
        //    lstTasks.ItemsSource = feeds;
        //}

        private void remoteXml_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            if (e.Error != null)
                return;
            XDocument xdoc = XDocument.Parse(e.Result);

            List<RssFeed> rssFeeds;

            rssFeeds = (from item in xdoc.Descendants("item")
                        select new RssFeed()
                        {
                            Title = item.Element("title").Value,
                            Content = item.Element("description").Value, 
                            URL = new Uri(item.Element("link").Value, UriKind.Absolute),
                        }).ToList();
            lstItems.ItemsSource = rssFeeds;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            string txtUri = textBox1.Text;
            txtUri = Uri.EscapeUriString(txtUri);
            Uri uri = new Uri(txtUri,UriKind.Absolute);
            remoteXml.DownloadStringAsync(uri);
        }

        private void HyperlinkButton_Click(object sender, RoutedEventArgs e)
        {
            
             
           // App.currentURL = new Uri
            NavigationService.Navigate(new Uri("/BrowserPage.xaml", UriKind.Relative));
        }
    }
}


the class RssFeed.cs is as shown below


using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Recipe1_RssReader
{
        public class RssFeed
        {
            public string Title { get; set; }
            public string Content { get; set; }
            public DateTime DatePosted { get; set; }
            public Uri URL { get; set; }
        }
   
}


the output can be shown on emulator after running the above project on device

the screen shot is given below 


you can download the source code from the link RSS-FEED




No comments:

Post a Comment