A simple RSS reader - the Avalon Way ;)

This is the PDC week... yoohoooo!!! So expect better things ... and you'll get them!!!

Its been a tight week with me busy trying to get things settled down... mmm .. yeah I am buying a house....So having heaps of papers before me... aahhh!!! that reminds me of onemore thing to do... But before i leave lets put a small sample here..

Ok ... now i was thinking of making a simple RSS Reader.... so here goes... the foundation aka software is the same as the last time...

How does it look like... Feast you eyes!!

 

Lets begin with the code:

--------------------------------------------------------------------------

My App.xaml

Same as before

--------------------------------------------------------------------------

Window1.xaml

 <

Window x:Class="rss_reader.Window1"

xmlns=https://schemas.microsoft.com/winfx/avalon/2005

xmlns:x=https://schemas.microsoft.com/winfx/xaml/2005

Text="rss_reader">

<

StackPanel Loaded="WindowLoaded" Width="250" Margin="5"
Background="VerticalGradient Beige wheat"> <!-- Gives the nice vertical gradient color-->

<

Label Content="Microsoft Times" Foreground="Black" FontFamily="Verdana"
FontSize="25" FontWeight="bold" HorizontalAlignment="Center"/>

<

Button Name ="b1" Click="ButtonClick" Width="70" HorizontalAlignment="Right"
Margin="10"> Next</Button>

<

TextBlock DockPanel.Dock="Top" MouseWheel="MouseWheel1" TextWrap="Wrap"
Margin="10" Background="white" Name="bl1" MouseLeftButtonDown="LeftButtonClick"
MouseRightButtonDown="RightButtonClick" ClipToBounds="True"></TextBlock>

<

TextBlock DockPanel.Dock="Bottom" MouseWheel="MouseWheel1" TextWrap="Wrap"
Margin="10" Background="yellow" Name="bl2" MouseLeftButtonDown="LeftButtonClick"
MouseRightButtonDown="RightButtonClick" ClipToBounds="True"></TextBlock>

<

TextBlock DockPanel.Dock="Bottom" MouseWheel="MouseWheel1" TextWrap="Wrap"
Margin="10" Background="pink" Name="bl3" MouseLeftButtonDown="LeftButtonClick"
MouseRightButtonDown="RightButtonClick" ClipToBounds="True"></TextBlock>

</

StackPanel>

</

Window>

So whats new here.... Lets see!!! You have 3 TextBlocks and each of them handle 3 events - Mouse wheel, Right Mouse Button click and Left Mouse Button Click

These just make the app look and feel better... Wait and watch :) ...

--------------------------------------------------------------------------

Window1.xaml.cs

 private void WindowLoaded(object sender, RoutedEventArgs e){   getRSSfeed(); }//Handles Left Mouse button click//The page has 3 blocks and everytime the TextBlock is clicked the Text size increasesprivate void LeftButtonClick(object sender, RoutedEventArgs e){         TextBlock bl1 = sender as TextBlock;         if (bl1.Name == "bl1")         {      //3 counters are kept to keep track of the current transfom of the TextBlock....
Right click reduces the font size                  incr = incr1;                  dcr = dcr1;         }         else if (bl1.Name == "bl2")         {                  incr = incr2;                  dcr = dcr2;            }            else if (bl1.Name == "bl3")            {                  incr = incr3;                  dcr = dcr3;            }            incr += 0.25;            //Here we scale the TextBlock            bl1.LayoutTransform = Transform.CreateScale(1 + incr - dcr, 1 + incr - dcr);            //Update the uncividual scale counters to the new value            if (bl1.Name == "bl1")            {          incr1 = incr;        }            else if (bl1.Name == "bl2")            {             incr2 = incr;           }            else if (bl1.Name == "bl3")            {              incr3 = incr;           }}
private void RightButtonClick(object sender, RoutedEventArgs e){            //This function is just a duplicate of the above but here we decrement the scale   }private void MouseWheel1(object sender, RoutedEventArgs e){         //Here we open up the URL of the RSS feed in a separate process            //so roll the wheel on the textBlock and you have the item opened in the browser                     TextBlock bl = sender as TextBlock;                     string url = (string)(bl.ToolTip);                     System.Diagnostics.Process.Start(url);}
private void ButtonClick(object sender, RoutedEventArgs e){              //Here we implement the logic for the next page of the RSS feed               //everything gets reset to the original value as we move to the next page of the feed             incr1 = incr2 = incr3 = dcr1 = dcr2 = dcr3 = 0;             getRSSfeed();             bl1.LayoutTransform = Transform.CreateScale(1, 1);             bl2.LayoutTransform = Transform.CreateScale(1, 1);             bl3.LayoutTransform = Transform.CreateScale(1, 1);}
private void getRSSfeed(){          //Function which gets the feed             string url = https://msdn.microsoft.com/rss.xml;            WebRequest req = WebRequest.Create(url);            WebResponse res = req.GetResponse();            Stream rsstream = res.GetResponseStream();            System.Xml.XmlDataDocument rssdoc = new System.Xml.XmlDataDocument();            rssdoc.Load(rsstream); //Loads the RSS stream into the XML doc            System.Xml.XmlNodeList rssitems = rssdoc.SelectNodes("rss/channel/item");   
//Give the path to the items interested in            string title = "";   //Here we look for only the title, description and link in the RSS stream            string description = "";            string link = "";            int count = 0, i;            for (i = _startIndex; i < rssitems.Count; i++)            {                        System.Xml.XmlNode rssdetail;                        rssdetail = rssitems.Item(i).SelectSingleNode("description");                        if (rssdetail != null)                       {            description = rssdetail.InnerText;       }                       else                       {             description = "";             }                       rssdetail = rssitems.Item(i).SelectSingleNode("title");                       if (rssdetail != null)                       {               title = rssdetail.InnerText;  }                       else                       {                title = ""; }                       rssdetail = rssitems.Item(i).SelectSingleNode("link");                       if (rssdetail != null)                       {              link = rssdetail.InnerText;       }                       else                       {              link = "";      }                       if (count == 0)                       {                                bl1.TextContent = title + "\r\n" + description;                                bl1.ToolTip = link;                       }                       else if (count == 1)                       {        //setup the TextBlock2       }                       else if (count == 2)                       {   //setup the TextBlock2     break; 
 //breaks the loop after 3 items since we display only 3 items at a time}                       count++;            }            _startIndex = i + 1;}
private int _startIndex = 0;private double incr = 0, dcr = 0, incr1 = 0, dcr1 = 0, incr2 = 0, dcr2 = 0, incr3 = 0, dcr3 = 0;} 

--------------------------------------------------------------------------

So there goes the code... Its functional and yeah theres some redundancy... and as i have said before we can have some optimization too... But the point here is to showcase Avalon and its ease of use....

So here we see the use of TextBlocks, the VerticalGradient, mouseEvents, RSS reader function calls, links......

Enjoy the weekend and keep the faith.