Bagikan melalui


RSS reader using a SinglePageViewer

Hey all, I have now moved into my new house and the reality is yet to sink in... Its been a pain unpacking all the stuff and moving things around. Yeah, this take time when you got to have things in all the right places and right angles.... I'll be posting some pics of the house in a couple of weeks..

So continuing with the RSS reader, here I'll show how it can be done using the singlePageViewer. Looks like I am getting fixated on RSS readers... ;)..not for long though. A singlePageViewer is a nice way of presenting stuff for read-only purposes. In the code that follows we will see how hyperlinks work and how we make use of templates.

To begin with heres how it looks like:

Image hosted by Photobucket.com

 We need to create a Browser app since Hyperlinks wont open unless they are hosted in a navigation element. So we cant have Hyperlinks in Windows and expect them to open a new window on clicking.

Once we have the barebone skeleton ready, we can start writing the code. This initial code is prepared automatically if we select the Avalon browser App template provided by VS extensions. Instead of having a Window we now have a Page.

MyApp.xaml and MyAPP.xaml.cs files remain as they are.

Page1.xaml

 <Page x:Class="AvalonApplication1.Page1"    xmlns=https://schemas.microsoft.com/winfx/avalon/2005    xmlns:x=https://schemas.microsoft.com/winfx/xaml/2005    Title="Page1" >
      <SinglePageViewer Loaded="PageLoaded" Name="spv"  >            <FlowDocument Name="FD"  PagePadding="10"  
ColumnWidth="435" ColumnGap="20" ColumnRuleBrush="Navy" ColumnRuleWidth="2" Background="beige">                  <FlowDocument.Resources>
                        <Style TargetType="{x:Type Hyperlink}">   <!--Sets the style for a link --> 
                              <Setter Property="FontSize" Value="18" />
                              <Style.Triggers>
                                    <Trigger Property="IsMouseOver" Value="True">
                                          <Setter Property="Foreground" Value="Red"/>
                                    </Trigger>
                              </Style.Triggers>
                        </Style>
                         <Style x:Key="BiologicalName" TargetType="{x:Type Paragraph}">  
  <!--Sets the style for a paragraph--> 

                              <Setter Property="FontSize" Value="16"/>
                              <Setter Property="FontStyle" Value="italic" />
                              <Setter Property="FontWeight" Value="500" />
                        </Style>
                  </FlowDocument.Resources>
                  <Paragraph TextAlignment="Center" FontSize="30"> 
 <!--Sets the Title and the Underline --> 

                        MSDN RSS Feed<LineBreak />
                        <Border >
                              <Border Background="DarkBlue" Height="2" Width="300" />
                        </Border>
                  </Paragraph>
            </FlowDocument>      </SinglePageViewer></Page>
 

Page1.xaml.cs

          private void PageLoaded(object sender, RoutedEventArgs e)        {            getRSSfeed();  <!-- On loading the Page get the RSS contents -->         }
        private void getRSSfeed()        {            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);            System.Xml.XmlNodeList rssitems = 
rssdoc.SelectNodes("rss/channel/item");            string title = "";            string description = "";            string link = "";            TextFlow tfl = new TextFlow();
            for (int i = 0; i < rssitems.Count; i++)            {                System.Xml.XmlNode rssdetail;                rssdetail = rssitems.Item(i).SelectSingleNode("title");                if (rssdetail != null)                {                    title = rssdetail.InnerText;                }                else                {                    title = "";                }                Hyperlink hl1 = new Hyperlink();
                 <!-- Set the URI of the Table of contents to the section having the content -->                  <!-- Sec1, Sec2,... are names of sections having the contents -->                 string str = "#Sec" + i.ToString();                hl1.NavigateUri = new Uri(str,UriKind.RelativeOrAbsolute);
                hl1.Text = title;                Paragraph p1 = new Paragraph(hl1);                FD.Blocks.Add(p1);          }
            Paragraph p21 = new Paragraph();            p21.BreakPageBefore = true;            FD.Blocks.Add(p21);
            for (int i = 0; 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 = "";                }                Hyperlink hl1 = new Hyperlink();
                if (link.Contains("http"))   <!-- Ensures that the link is proper. <br>Otherwise throws a security exception -->                 {                    hl1.NavigateUri = new Uri(link);                }
                hl1.Text = title;                Paragraph p1 = new Paragraph(hl1);                p1.Name = i.ToString();                FD.Blocks.Add(p1);                Section sec = new Section();
                sec.Name = "Sec" + i.ToString();
                Paragraph p2 = new Paragraph(new Run(description));                p2.SetResourceReference(Paragraph.StyleProperty, 
"BiologicalName");                sec.Blocks.Add(p2);                FD.Blocks.Add(sec);            }            FD.BringIntoView();  <!-- Displays the contents -->         }
 
 

Thats all there is to the code. I have highlighted only the code that has been added to this app. The rest of the code above has been explained in the RSS app i wrote about previously :)

I'll be posting some more bytes soon!! Ciao for now. :)