Windows Phone开发基础(11)实现一个RSS阅读器
生活随笔
收集整理的這篇文章主要介紹了
Windows Phone开发基础(11)实现一个RSS阅读器
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實現一個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截圖工具誕生攻堅實錄(三)
呵呵 支持(0)反對(0) ??回復引用
自己手動引用一個就可以了 支持(0)反對(0) ??回復引用
如何手動添加一個啊,貌似安裝盤的Servicemodel.dll程序集不支持這個 支持(0)反對(0) ??回復引用
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??
?
..... 支持(0)反對(0) ??回復引用?
#2樓[樓主]?2011-06-27 15:39?linzheng??
?
@chenkai呵呵 支持(0)反對(0) ??回復引用
?
#3樓?2011-07-15 16:13?lqy0326[未注冊用戶]
?
怎么我沒有找到這個對象啊 .. 引用了servicemodel都沒有. ??回復引用?
#4樓?2011-07-19 18:31?yjzh??
?
@lqy0326自己手動引用一個就可以了 支持(0)反對(0) ??回復引用
?
#5樓?2011-07-26 10:19?逍遙無極??
?
@yjzh如何手動添加一個啊,貌似安裝盤的Servicemodel.dll程序集不支持這個 支持(0)反對(0) ??回復引用
?
#6樓?2011-07-26 11:05?yjzh??
?
C:\Program Files\Microsoft SDKs\Silverlight\v4.0\Libraries\Client\System.ServiceModel.Syndication.dll直接引用這個,引用的時候會有一個警告,不用理它,然后就可以用了 支持(0)反對(0) ??回復引用?
#7樓?2011-11-13 16:40?gdxzkid[未注冊用戶]
?
編碼問題怎么解決?sydcation好像只支持utf-8,像gb2312都不支持 ??回復引用?
?
轉載于:https://www.cnblogs.com/Belling/archive/2013/03/31/2991899.html
總結
以上是生活随笔為你收集整理的Windows Phone开发基础(11)实现一个RSS阅读器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 登陆sqlserver及修改端口号
- 下一篇: 毕业离校,新的开始