实现一个RSS阅读器,通过你输入的RSS地址来获取RSS的信息列表和查看RSS文章中的详细内容。RSS阅读器是使用了WebClient类来获取网络上的RSS的信息,然后再转化为自己定义好的RSS实体类对象的列表,最后绑定到页面上。
(1)       RSS实体类和RSS服务类
RssItem.cs

using System.Net;using System.Text.RegularExpressions;

namespace WindowsPhone.Helpers{///<summary>/// RSS对象类///</summary>publicclass RssItem    {///<summary>/// 初始化一个RSS目录///</summary>///<param name="title">标题</param>///<param name="summary">内容</param>///<param name="publishedDate">发表事件</param>///<param name="url">文章地址</param>public RssItem(string title, string summary, string publishedDate, string url)        {            Title = title;            Summary = summary;            PublishedDate = publishedDate;            Url = url;

//解析html            PlainSummary = HttpUtility.HtmlDecode(Regex.Replace(summary, "<[^>]+?>", ""));        }

//标题publicstring Title { get; set; }

//内容publicstring Summary { get; set; }

//发表时间publicstring PublishedDate { get; set; }

//文章地址publicstring Url { get; set; }

//解析的文本内容publicstring PlainSummary { get; set; }    }}

RssService.cs
using System;using System.Collections.Generic;using System.IO;using System.Net;using System.ServiceModel.Syndication;using System.Xml;

namespace WindowsPhone.Helpers{///<summary>/// 获取网络RSS服务类///</summary>publicstaticclass RssService    {

///<summary>/// 获取RSS目录列表///</summary>///<param name="rssFeed">RSS的网络地址</param>///<param name="onGetRssItemsCompleted">获取完成事件</param>publicstaticvoid GetRssItems(string rssFeed, Action<IEnumerable<RssItem>> onGetRssItemsCompleted =null, Action<Exception> onError =null, Action onFinally =null)        {            WebClient webClient =new WebClient();

//注册webClient读取完成事件            webClient.OpenReadCompleted +=delegate(object sender, OpenReadCompletedEventArgs e)            {try                {

if (e.Error !=null)                    {if (onError !=null)                        {                            onError(e.Error);                        }return;                    }

//将网络获取的信息转化成RSS实体类                    List<RssItem> rssItems =new List<RssItem>();                    Stream stream = e.Result;                    XmlReader response = XmlReader.Create(stream);                    SyndicationFeed feeds = SyndicationFeed.Load(response);foreach (SyndicationItem f in feeds.Items)                    {                        RssItem rssItem =new RssItem(f.Title.Text, f.Summary.Text, f.PublishDate.ToString(), f.Links[0].Uri.AbsoluteUri);                        rssItems.Add(rssItem);                    }

//通知完成返回事件执行if (onGetRssItemsCompleted !=null)                    {                        onGetRssItemsCompleted(rssItems);                    }                }finally                {if (onFinally !=null)                    {                        onFinally();                    }                }            };

            webClient.OpenReadAsync(new Uri(rssFeed));        }    }}


(2)       RSS页面展示
MainPage.xaml
<phone:PhoneApplicationPage x:Class="ReadRssItemsSample.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">

<Grid x:Name="LayoutRoot" Background="Transparent"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="*"/></Grid.RowDefinitions>

<StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28"><TextBlock x:Name="ApplicationTitle" Text="RSS阅读器" Style="{StaticResource PhoneTextNormalStyle}"/></StackPanel>

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition  Height="*"/></Grid.RowDefinitions><TextBlock FontSize="30" Grid.Row="1" Height="49" HorizontalAlignment="Left" Margin="0,6,0,0" Name="textBlock1" Text="RSS地址" VerticalAlignment="Top" Width="116"/><TextBox Grid.Row="1" Height="72" HorizontalAlignment="Left" Margin="107,0,0,0" Name="rssURL" Text="http://www.cnblogs.com/rss" VerticalAlignment="Top" Width="349"/><Button Content="加载 RSS" Click="Button_Click" Margin="-6,72,6,552" Grid.Row="1"/><ListBox x:Name="listbox" Grid.Row="1" SelectionChanged="OnSelectionChanged" Margin="0,150,6,-11"><ListBox.ItemTemplate  ><DataTemplate><Grid><Grid.RowDefinitions><RowDefinition Height="Auto"/><RowDefinition Height="Auto"/><RowDefinition Height="60"/></Grid.RowDefinitions><TextBlock Grid.Row="0" Text="{Binding Title}" Foreground="Blue"/><TextBlock Grid.Row="1" Text="{Binding PublishedDate}" Foreground="Green"/><TextBlock Grid.Row="2" TextWrapping="Wrap" Text="{Binding PlainSummary}"/></Grid></DataTemplate></ListBox.ItemTemplate></ListBox></Grid></Grid>

</phone:PhoneApplicationPage>

MainPage.xaml.cs
using System.Windows;using System.Windows.Controls;using Microsoft.Phone.Controls;using WindowsPhone.Helpers;

namespace ReadRssItemsSample{publicpartialclass MainPage : PhoneApplicationPage    {privatestring WindowsPhoneBlogPosts ="";

public MainPage()        {            InitializeComponent();        }

privatevoid Button_Click(object sender, RoutedEventArgs e)        {if (rssURL.Text !="")            {                WindowsPhoneBlogPosts = rssURL.Text;            }else            {                MessageBox.Show("请输入RSS地址!");return;            }//加载RSS列表            RssService.GetRssItems(                WindowsPhoneBlogPosts,                (items) => { listbox.ItemsSource = items; },                (exception) => { MessageBox.Show(exception.Message); },null                );        }

//查看文章的详细内容privatevoid OnSelectionChanged(object sender, SelectionChangedEventArgs e)        {if (listbox.SelectedItem ==null)return;

            var template = (RssItem)listbox.SelectedItem;

            MessageBox.Show(template.PlainSummary);

            listbox.SelectedItem =null;        }

    }}

(3)程序运行的效果如下

标签: Windows Phone 7
绿色通道: 好文要顶 关注我 收藏该文与我联系 

linzheng
关注 - 24
粉丝 - 456

+加关注

4
0
(请您对文章做出评价)

« 博主上一篇:Windows Phone 7 网络编程之使用Socket(芒果更新)
» 博主下一篇:Windows Phone 7 如何判断ListBox控件滚动到底
« 首页上一篇:【创业】创业团队的那些事(二)
» 首页下一篇:我让鬼佬吃醋了:WP7截图工具诞生攻坚实录(三)

posted on 2011-06-27 00:11 linzheng 阅读(2447) 评论(7) 编辑 收藏

评论

#1楼 2011-06-27 00:31 chenkai

回复引用

#2楼[楼主] 2011-06-27 15:39 linzheng

回复引用

#3楼 2011-07-15 16:13 lqy0326[未注册用户]

回复引用

#4楼 2011-07-19 18:31 yjzh

回复引用

#5楼 2011-07-26 10:19 逍遥无极

回复引用

#6楼 2011-07-26 11:05 yjzh

回复引用

#7楼 2011-11-13 16:40 gdxzkid[未注册用户]

回复引用

转载于:https://www.cnblogs.com/Belling/archive/2013/03/31/2991899.html

Windows Phone开发基础(11)实现一个RSS阅读器相关推荐

  1. 一个RSS阅读器的源码,不敢独享!

    最近有朋友要我帮忙弄一个在线Rss阅读器,找了不少资料,没有完整项目文件,我就根据资料弄了一个 把第一版分享给大家. 你可以随意输入符合标准的Rss地址  代码内含有详细注释,这个只是一个简单的Asp ...

  2. 如何自己手动搭建一个RSS订阅机器人(rssbot),自己做一个RSS阅读器

    当你想RSS订阅一些自己感兴趣的博客,却又苦于免费的RSS阅读器广告很多时,可以自己借助Telegram机器人搭建一个RSS订阅机器人.本文老王介绍下如何搭建一个Telegram RSS订阅机器人,以 ...

  3. UWP 推荐 - 限时免费的RSS阅读器《RSS 追踪》登录 Windows 10

    文/云之幻 前不久,博客作者 Bravo Yeung 写了一篇还算略受欢迎的关于 RSS 的文章 .Net开发者必知的技术类RSS订阅指南. RSS 现在用的人很少了,而且就算是我,也不过是在一周前才 ...

  4. UWP 推荐 | 限时免费的RSS阅读器《RSS 追踪》登录 Windows 10

    前不久,本公号作者 Bravo Yeung 写了一篇不错的关于 RSS 的文章 .Net开发者必知的技术类RSS订阅指南. RSS 现在用的人很少了,而且就算是我,也不过是在一周前才开始正视 RSS ...

  5. 用vb.net写一个简易的RSS阅读器

    先发一个做出来的效果图 哈哈!怎么样?对了DUDU,我这个还解决了看天下那个阅读器不能正常显示相对路径的图片的问题哟! 现在不管相对的还是绝对的路径的图片都能正常显示哈! 下面是源代码,没有太多注释, ...

  6. Emacs中的RSS阅读器--newsticker

    1 简介 ------- newsticker是一个RSS阅读器,它支持以下几种格式 * RSS 0.91 * RSS 0.92 * RSS 1.0 * RSS 2.0 * Atom 0.3 * At ...

  7. PHP 实例 - AJAX RSS 阅读器

    PHP 实例 - AJAX RSS 阅读器 RSS 阅读器用于阅读 RSS Feed. AJAX RSS 阅读器 在下面的实例中,我们将演示一个 RSS 阅读器,通过它,来自 RSS 的内容在网页不进 ...

  8. Linux下的RSS阅读器(转)

    Linux下的RSS阅读器(转) 网络内容"推"技术是新一代互联网发展的必然趋势.它为信息发布者和接收者提供了很多方便,大大降低了信息流通的成本. RSS的出现改变了互联网内容的传 ...

  9. 苹果上最好用的 RSS 阅读器客户端

    Reeder是一个RSS阅读器,最初为iPhone,现在可用于Mac.Reeder是一个简单的Mac OS X客户端,用于Feedbin,Feedly,饲料牧马人,发烧和可读性,也可以作为一个独立的新 ...

最新文章

  1. 用74l138实现一个一位全减器_用pygame实现一个简单的五子棋游戏
  2. 如何成为一名专家级的开发人员
  3. NAT概念解释(不完全版,但不会搞错...)
  4. 连接MYSQL的时候报错(找不到请求的.net framework data provider。可能没有安装
  5. Linux下安装FTP
  6. VSCode 更新后打不开之解决办法
  7. 一串字符串转换为ascii_将ASCII字符串(char [])转换为C中的BYTE数组
  8. 【Java并发编程】之二:线程中断
  9. HTML复选框可以设置为只读吗?
  10. 基于MATLAB的数字水印技术实现解析
  11. 浙大吴飞“舌战”阿里贾扬清:AI内卷与年薪百万,哪个才是真实?
  12. STM32__04—PMW呼吸灯
  13. Redis客户端访问
  14. throws和throw的作用
  15. 2022年武汉市创新型中小企业认定条件和评价指标
  16. 蓝桥杯每日积累-打破算法壁垒,攻略2n皇后问题
  17. spring boot 怎么 html 嵌套 html?
  18. mac写作软件哪个好?推荐几款实用的mac写作软件
  19. 兰海说成长|孩子不爱做作业怎么办?
  20. 二进制算法_本地二进制模式算法:其背后的数学❗️

热门文章

  1. 训练MNIST数据集模型
  2. 计算机视觉:让冰冷的机器看懂多彩的世界
  3. 一维随机变量及其概率分布
  4. react hook——你可能不是“我”所认识的useEffect
  5. import提升导致Fundebug报错:“请配置apikey”
  6. win10设置默认打开方式
  7. EonerCMS——做一个仿桌面系统的CMS(十一)
  8. JSP显示错误信息中四个范围来保存变量
  9. 使用模板将Web服务的结果转换为标记语言
  10. linux工作常用软件