Subscribe to andrewsw's Blog        RSS Feed
-----

RSS Feed in WPF

Icon 1 Comments
I found this cool page to display RSS feed items in WPF:

Build an RSS reader in 5 minutes

It works great. I thought I would see how it behaves with dream.in.code's feed:

http://feeds.feedbur...ured?format=xml

Posted Image

Unfortunately it, for me, produces a Script Error dialog several times. My settings in IE are already set to not display this dialog. I suppose we could just press "No", don't run any further scripts, but I investigated further to see if I could disable the dialog.

I found some code that attempts to disable the dialog but for a WebBrowser control. I then found the following link that converts a Frame to a WebBrowser, so that I could then attempt the code to disable the dialog.

WPF WebBrowser Control and the Document Object

Here is the code-behind that attempts the disabling:
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Syndication; // add this
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;

namespace TestFeedsWPF {
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public MainWindow() {
            InitializeComponent();
        }

        // http://www.thomaslevesque.com/2009/02/13/build-an-rss-reader-in-5-minutes/
        private void btnGo_Click(object sender, RoutedEventArgs e) {
            using (XmlReader reader = XmlReader.Create(txtUrl.Text)) {
                SyndicationFeed feed = SyndicationFeed.Load(reader);
                lstFeedItems.ItemsSource = feed.Items;
            }
        }

        private void frmContents_ContentRendered(object sender, EventArgs e) {
            // http://www.rhizohm.net/irhetoric/post/2008/11/12/0a-WPF-WebBrowser-Control-and-the-Document-Object0a-.aspx
            // Add Reference MS HTML Object Library
            if (frmContents.Content.GetType() == typeof(WebBrowser)) {
                WebBrowser browser = (WebBrowser)frmContents.Content;
                mshtml.HTMLDocument dom = (mshtml.HTMLDocument)browser.Document;
                if (browser == null || browser.Document == null)
                    return;

                dynamic document = browser.Document;

                //if (document.readyState != "complete") // is this needed?
                //    return;

                dynamic script = document.createElement("script");
                script.type = @"text/javascript";
                script.text = @"window.onerror = function(msg,url,line){return true;}";
                document.head.appendChild(script);
            } 
        }
    }
}


Here's the XAML (very similar to that in the original link above):
<Window x:Class="TestFeedsWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="RSS Feed Reader" Height="400" Width="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button Name="btnGo"
                    DockPanel.Dock="Right"
                    Width="50"
                    Content="Go"
                    Click="btnGo_Click" />
            <TextBox Name="txtUrl" Text="Address here" />
            <!--http://feeds.feedburner.com/dic_featured?format=xml-->
        </DockPanel>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Name="lstFeedItems"
                     Grid.Column="0"
                     DisplayMemberPath="Title.Text" />
            <GridSplitter Grid.Column="1"
                          VerticalAlignment="Stretch"
                          Width="3"
                          Resizebehavior="PreviousAndNext"
                          ResizeDirection="Columns"/>
            <Frame Name="frmContents"
                   Source="{Binding SelectedItem.Links[0].Uri, ElementName=lstFeedItems}"
                   Grid.Column="2" ContentRendered="frmContents_ContentRendered"
                   NavigationUIVisibility="Visible">
            </Frame>
        </Grid>
    </Grid>
</Window>


Well, as you may have guessed by now, the attempt to disable the script dialog doesn't work. Anyway, if you don't mind responding to the dialog, then you could take this and make it look more attractive! Otherwise, if anyone knows how the dialog might be disabled, then I'll be pleased to hear from them. Andy.

PS I suppose it would also be possible to click a title on the left and have the itemcontent (the Summary) appear on the right, but I haven't pursued this.

1 Comments On This Entry

Page 1 of 1

andrewsw 

18 February 2015 - 11:19 AM
Hey, I managed to get the Summary to appear on the right! Well, in a rough-and-ready way. It isn't fully HTML formatted, but it does the job of displaying a Summary (fast and without errors).

Posted Image

FeedItem.cs
namespace TestFeedsWPF {
    public class FeedItem {
        public string Title { get; set; }
        private string _summary;
        public string Summary {
            get {
                //return "<html><body>" + _summary + "</body></html>";
                return _summary;
            }
            set {
                _summary = value;
            }
        }
    }
}

Mainwindow.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Syndication; // add this
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using TestFeedsWPF;

namespace TestFeedsWPF {
    /// <summary>
    /// Interaction logic for Mainwindow.xaml
    /// </summary>
    public partial class MainWindow : Window {
        public List<FeedItem> items = new List<FeedItem>();

        public MainWindow() {
            InitializeComponent();
        }

        // http://www.thomaslevesque.com/2009/02/13/build-an-rss-reader-in-5-minutes/
        private void btnGo_Click(object sender, RoutedEventArgs e) {
            using (XmlReader reader = XmlReader.Create(txtUrl.Text)) {
                SyndicationFeed feed = SyndicationFeed.Load(reader);
                foreach (var item in feed.Items) {
                    items.Add(new FeedItem() {
                        Title = item.Title.Text,
                        Summary = item.Summary.Text
                    });
                }
                lstFeedItems.ItemsSource = items;
            }
        }

        private void lstFeedItems_Selectionchanged(object sender, SelectionchangedEventArgs e) {
            wbContents.NavigateToString(((FeedItem)lstFeedItems.SelectedItem).Summary);
        }
    }
}

Mainwindow.xaml
<Window x:Class="TestFeedsWPF.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="RSS Feed Reader" Height="400" Width="600">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <DockPanel Grid.Row="0">
            <Button Name="btnGo"
                    DockPanel.Dock="Right"
                    Width="50"
                    Content="Go"
                    Click="btnGo_Click" />
            <TextBox Name="txtUrl" Text="Address here" />
            <!--http://feeds.feedburner.com/dic_featured?format=xml-->
        </DockPanel>
        <Grid Grid.Row="1">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="250"/>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="*"/>
            </Grid.ColumnDefinitions>
            <ListBox Name="lstFeedItems" Selectionchanged="lstFeedItems_Selectionchanged"
                     Grid.Column="0"
                     DisplayMemberPath="Title" />
            <GridSplitter Grid.Column="1"
                          VerticalAlignment="Stretch"
                          Width="3"
                          Resizebehavior="PreviousAndNext"
                          ResizeDirection="Columns"/>
            <WebBrowser Name="wbContents" Grid.Column="2"></WebBrowser>
        </Grid>
    </Grid>
</Window>


I had to switch from a Frame to a WebBrowser so that I could use its NavigateToString method.
0
Page 1 of 1

Trackbacks for this entry [ Trackback URL ]

There are no Trackbacks for this entry

December 2019

S M T W T F S
1234567
8910111213 14
15161718192021
22232425262728
293031    

Tags

    Recent Entries

    Recent Comments

    Search My Blog

    1 user(s) viewing

    1 Guests
    0 member(s)
    0 anonymous member(s)

    Categories