在这篇文章中我们将为 Windows Phone 7 手机创建一个简单的 RSS (Really Simple Syndication) 阅读器。用户界面包含一个文本输入框用于输入 RSS 地址,以及一个 ListBox 用于显示 RSS 条目,如下图所示:

下载整个项目打包
项目是详细的步骤:
1. 创建新项目
首先我们要创建一个新项目,打开 Visual Studio 2010 -> File -> New Project -> 选择 Windows Phone Application ,如下图所示:

2. 为项目增加引用
接下来你需要增加对 Microsoft.Xml.Linq 这个 XML 解析库的应用,右击项目下的引用库,人然后选择添加引用,在 .NET 的 tab 页中你将找到 Microsoft.Xml.Linq, 点击 OK 按钮,如下图所示:

3. 修改标题
这个步骤不是必须的,但我还是决定加进来,在这个步骤中我们简单的修改 TitleGrid 元素用来指定应用程序的标题
1 |
< StackPanel x:Name = "TitlePanel" Grid.Row = "0" Margin = "24,24,0,12" > |
2 |
< TextBlock x:Name = "ApplicationTitle" Text = "EUGENEDOTNET.COM" Margin = "0,0,0,0" Style = "{StaticResource PhoneTextNormalStyle}" /> |
3 |
< TextBlock x:Name = "PageTitle" Text = "RSS Reader" Margin = "-3,-8,0,0" Style = "{StaticResource PhoneTextTitle1Style}" /> |
4. 修改 ContentGrid
ContentGrid 将包含应用程序中所需的 UI 元素,最基本的包括下面三项:
- TextBox 用于输入 RSS 网址
- Button 用于触发读取 RSS 信息的事件
- ListBox 用于显示 RSS 数据结果,包含标题和链接
需要注意的是按钮的点击事件是通过 XAML 中定义的,另外 Windows Phone 7 有一个新的 InputScope 特性用于简化输入框直接输入 URL 地址后响应回车键。
01 |
< Grid x:Name = "ContentGrid" Grid.Row = "1" > |
04 |
< RowDefinition Height = "70" /> |
05 |
< RowDefinition Height = "Auto" /> |
06 |
</ Grid.RowDefinitions > |
08 |
< TextBox Name = "tbRSSFeedURL" Grid.Row = "0" Text = "" Width = "350" Margin = "10,0,10,0" HorizontalAlignment = "Left" > |
12 |
< InputScopeName NameValue = "Url" /> |
17 |
< Button Name = "btnReadFeed" Grid.Row = "0" Content = "Read" Width = "110" Margin = "10,0,10,0" HorizontalAlignment = "Right" Click = "btnReadFeed_Click" /> |
19 |
< ScrollViewer Width = "440" Height = "530" Grid.Row = "1" Margin = "10, 20, 0, 0" > |
20 |
< ListBox Name = "listboxRSSFeedItems" Width = "440" Height = "530" > |
22 |
< ListBox.ItemTemplate > |
26 |
< RowDefinition Height = "Auto" /> |
28 |
< RowDefinition Height = "Auto" /> |
29 |
</ Grid.RowDefinitions > |
30 |
< TextBlock TextWrapping = "Wrap" Text = "{Binding Title}" Grid.Row = "0" FontSize = "24" HorizontalAlignment = "Left" /> |
32 |
< HyperlinkButton Content = "Link to details" NavigateUri = "{Binding Link}" HorizontalAlignment = "Left" Grid.Row = "1" Margin = "0,0,0,30" /> |
36 |
</ ListBox.ItemTemplate > |
5. 为 RSS 条目创建一个单独的类
现在我们只需要创建一个新的容器类用来保存 RSS 数据
1 |
public class RSSFeedItem |
3 |
public string Title { get ; set ; } |
4 |
public Uri URL { get ; set ; } |
6. 实现事件功能
接下来我们需要为 Read 按钮编写事件处理程序,我们需要创建一个新的 URI 对象并传递给 ReadRss 方法
1 |
private void btnReadFeed_Click( object sender, RoutedEventArgs e) |
3 |
if (! string .IsNullOrEmpty(tbRSSFeedURL.Text)) |
5 |
ReadRss( new Uri(tbRSSFeedURL.Text, UriKind.Absolute)); |
7. 实现应用程序逻辑
需要确保在 MainPage.xaml 类中使用如下应用语句:
接下来你需要实现一个 ReadRss 方法,接收一个 URI 参数。如果请求成功的话,应用将处理返回的 RSS 信息并将每个条目放到 ListBox 中
01 |
public void ReadRss(Uri rssUri) |
03 |
WebClient wclient = new WebClient(); |
05 |
wclient.OpenReadCompleted += (sender, e) => |
10 |
Stream str = e.Result; |
11 |
XDocument xdoc = XDocument.Load(str); |
14 |
List<RSSFeedItem> rssFeedItems = (from item in xdoc.Descendants( "item" ) |
15 |
select new RSSFeedItem() |
17 |
Title = item.Element( "title" ).Value, |
18 |
URL = new Uri(item.Element( "link" ).Value, UriKind.Absolute), |
24 |
listboxRSSFeedItems.Items.Clear(); |
25 |
rssFeedItems.ForEach(item => listboxRSSFeedItems.Items.Add(item)); |
27 |
wclient.OpenReadAsync(rssUri); |