如何为 Windows Phone 8 创建基本的 RSS 阅读器
1个回答
展开全部
如何为 Windows Phone 8 创建基本的 RSS 阅读器
适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone OS 7.1
您可以通过完成以下步骤创建基本 RSS 阅读器。此处展示的指南和代码示例基于名为 RSS 阅读器示例的代码示例。
本主题包括以下部分。
创建 Windows Phone 应用项目
向整合 DLL 中添加引用
创建 RSS 文本裁边器
更新 XAML 代码
更新代码隐藏文件
相关主题
创建 Windows Phone 应用项目
在 Visual Studio 中创建新的 Windows?0?2Phone 应用 项目。
向整合 DLL 中添加引用
若要使用 SyndicationFeed 类,您必须首先添加 DLL 引用。
向整合 DLL 中添加引用
在 Visual Studio 的“项目”菜单中,选择“添加引用”,然后选择“浏览”选项卡。
导航到“程序文件 (x86)”目录。
导航到 Microsoft SDKs/Silverlight/v4.0/Libraries/Client/。
选择“System.ServiceModel.Syndication.dll”,然后单击“确定”。
注意:
如果在此步骤中您查看到一个询问您是否希望继续的提示,则单击“是”。
创建 RSS 文本裁边器
RSS 源中的说明文本通常包含您可能不希望在 RSS 阅读器中显示的 HTML、编码字符及其他数据。您可以从说明文本创建一个排除 HTML 及其他不需要数据的类,并将该类设置为 XAML 字段的转换器。
创建 RSS 文本裁边器
在“解决方案资源管理器”中,右键单击项目名称,再单击“添加”,然后单击“新建项”。
在“已安装的模板”窗格中,选择“Visual C#”,然后在中间窗格内选择“类”。
命名文件 RssTextTrimmer.cs,然后单击“添加”。
在解决方案资源管理器中,双击 RssTextTrimmer.cs 文件。
添加以下命名空间:
C#
using System.Windows.Data;
using System.Globalization;
using System.Text.RegularExpressions;
更新类定义,以便它可以实现 IValueConverter 接口:
C#
public class RssTextTrimmer : IValueConverter
在 RssTextTrimmer 类中添加以下代码:
C#
// Clean up text fields from each SyndicationItem.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
int maxLength = 200;
int strLength = 0;
string fixedString = "";
// Remove HTML tags and newline characters from the text, and decode HTML encoded characters.
// This is a basic method. Additional code would be needed to more thoroughly
// remove certain elements, such as embedded Javascript.
// Remove HTML tags.
fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty);
// Remove newline characters.
fixedString = fixedString.Replace("\r", "").Replace("\n", "");
// Remove encoded HTML characters.
fixedString = HttpUtility.HtmlDecode(fixedString);
strLength = fixedString.ToString().Length;
// Some feed management tools include an image tag in the Description field of an RSS feed,
// so even if the Description field (and thus, the Summary property) is not populated, it could still contain HTML.
// Due to this, after we strip tags from the string, we should return null if there is nothing left in the resulting string.
if (strLength == 0)
{
return null;
}
// Truncate the text if it is too long.
else if (strLength >= maxLength)
{
fixedString = fixedString.Substring(0, maxLength);
// Unless we take the next step, the string truncation could occur in the middle of a word.
// Using LastIndexOf we can find the last space character in the string and truncate there.
fixedString = fixedString.Substring(0, fixedString.LastIndexOf(" "));
}
fixedString += "...";
return fixedString;
}
// This code sample does not use TwoWay binding, so we do not need to flesh out ConvertBack.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
通过在“解决方案资源管理器”中双击“App.xaml”,然后在 <Application.Resources> 标记中添加以下代码将 RssTextTrimmer 类设置为转换器:
XAML
<converter:RssTextTrimmer xmlns:converter="clr-namespace:namespace" x:Key="RssTextTrimmer" />
其中,namespace 是项目的命名空间的名称。例如,在 Windows Phone 8 示例的基本 RSS 阅读器中,namespace 被替换为 sdkBasicRSSReaderWP8CS。
更新 XAML 代码
更新 XAML 代码
在“解决方案资源浏览器”中双击 MainPage.xaml 文件。
将 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> 替换为下面的 XAML 代码:
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,12,0">
<Button Content="Load Feed" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="loadFeedButton" VerticalAlignment="Top" Width="273" Click="loadFeedButton_Click" />
<ListBox Name="feedListBox" Height="468" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="444" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top">
<TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedSummary" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border BorderBrush="{StaticResource PhoneSubtleBrush}" BorderThickness="1" Height="2" HorizontalAlignment="Left" Margin="20,88,0,0" Name="border1" VerticalAlignment="Top" Width="438" />
</Grid>
更新代码隐藏文件
更新代码隐藏文件的步骤
添加以下命名空间:
C#
using System.IO;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;
将以下代码添加到 MainPage 类中,放在 MainPage 构造函数后面。
注意:
以下代码中还包括对基本逻辑删除的支持。
C#
// Click handler that runs when the 'Load Feed' or 'Refresh Feed' button is clicked.
private void loadFeedButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
// WebClient is used instead of HttpWebRequest in this code sample because
// the implementation is simpler and easier to use, and we do not need to use
// advanced functionality that HttpWebRequest provides, such as the ability to send headers.
WebClient webClient = new WebClient();
// Subscribe to the DownloadStringCompleted event prior to downloading the RSS feed.
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
// Download the RSS feed. DownloadStringAsync was used instead of OpenStreamAsync because we do not need
// to leave a stream open, and we will not need to worry about closing the channel.
webClient.DownloadStringAsync(new System.Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx"));
}
// Event handler which runs after the feed is fully downloaded.
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Showing the exact error message is useful for debugging. In a finalized application,
// output a friendly and applicable string to the user instead.
MessageBox.Show(e.Error.Message);
});
}
else
{
// Save the feed into the State property in case the application is tombstoned.
this.State["feed"] = e.Result;
UpdateFeedList(e.Result);
}
}
// This method determines whether the user has navigated to the application after the application was tombstoned.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// First, check whether the feed is already saved in the page state.
if (this.State.ContainsKey("feed"))
{
// Get the feed again only if the application was tombstoned, which means the ListBox will be empty.
// This is because the OnNavigatedTo method is also called when navigating between pages in your application.
// You would want to rebind only if your application was tombstoned and page state has been lost.
if (feedListBox.Items.Count == 0)
{
UpdateFeedList(State["feed"] as string);
}
}
}
// This method sets up the feed and binds it to our ListBox.
private void UpdateFeedList(string feedXML)
{
// Load the feed into a SyndicationFeed instance.
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
// In Windows Phone OS 7.1 or later versions, WebClient events are raised on the same type of thread they were called upon.
// For example, if WebClient was run on a background thread, the event would be raised on the background thread.
// While WebClient can raise an event on the UI thread if called from the UI thread, a best practice is to always
// use the Dispatcher to update the UI. This keeps the UI thread free from heavy processing.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox.
feedListBox.ItemsSource = feed.Items;
loadFeedButton.Content = "Refresh Feed";
});
}
// The SelectionChanged handler for the feed items
private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
// Get the SyndicationItem that was tapped.
SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
// Set up the page navigation only if a link actually exists in the feed item.
if (sItem.Links.Count > 0)
{
// Get the associated URI of the feed item.
Uri uri = sItem.Links.FirstOrDefault().Uri;
// Create a new WebBrowserTask Launcher to navigate to the feed item.
// An alternative solution would be to use a WebBrowser control, but WebBrowserTask is simpler to use.
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = uri;
webBrowserTask.Show();
}
}
}
适用于:Windows Phone 8 和 Windows Phone Silverlight 8.1 | Windows Phone OS 7.1
您可以通过完成以下步骤创建基本 RSS 阅读器。此处展示的指南和代码示例基于名为 RSS 阅读器示例的代码示例。
本主题包括以下部分。
创建 Windows Phone 应用项目
向整合 DLL 中添加引用
创建 RSS 文本裁边器
更新 XAML 代码
更新代码隐藏文件
相关主题
创建 Windows Phone 应用项目
在 Visual Studio 中创建新的 Windows?0?2Phone 应用 项目。
向整合 DLL 中添加引用
若要使用 SyndicationFeed 类,您必须首先添加 DLL 引用。
向整合 DLL 中添加引用
在 Visual Studio 的“项目”菜单中,选择“添加引用”,然后选择“浏览”选项卡。
导航到“程序文件 (x86)”目录。
导航到 Microsoft SDKs/Silverlight/v4.0/Libraries/Client/。
选择“System.ServiceModel.Syndication.dll”,然后单击“确定”。
注意:
如果在此步骤中您查看到一个询问您是否希望继续的提示,则单击“是”。
创建 RSS 文本裁边器
RSS 源中的说明文本通常包含您可能不希望在 RSS 阅读器中显示的 HTML、编码字符及其他数据。您可以从说明文本创建一个排除 HTML 及其他不需要数据的类,并将该类设置为 XAML 字段的转换器。
创建 RSS 文本裁边器
在“解决方案资源管理器”中,右键单击项目名称,再单击“添加”,然后单击“新建项”。
在“已安装的模板”窗格中,选择“Visual C#”,然后在中间窗格内选择“类”。
命名文件 RssTextTrimmer.cs,然后单击“添加”。
在解决方案资源管理器中,双击 RssTextTrimmer.cs 文件。
添加以下命名空间:
C#
using System.Windows.Data;
using System.Globalization;
using System.Text.RegularExpressions;
更新类定义,以便它可以实现 IValueConverter 接口:
C#
public class RssTextTrimmer : IValueConverter
在 RssTextTrimmer 类中添加以下代码:
C#
// Clean up text fields from each SyndicationItem.
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
if (value == null) return null;
int maxLength = 200;
int strLength = 0;
string fixedString = "";
// Remove HTML tags and newline characters from the text, and decode HTML encoded characters.
// This is a basic method. Additional code would be needed to more thoroughly
// remove certain elements, such as embedded Javascript.
// Remove HTML tags.
fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty);
// Remove newline characters.
fixedString = fixedString.Replace("\r", "").Replace("\n", "");
// Remove encoded HTML characters.
fixedString = HttpUtility.HtmlDecode(fixedString);
strLength = fixedString.ToString().Length;
// Some feed management tools include an image tag in the Description field of an RSS feed,
// so even if the Description field (and thus, the Summary property) is not populated, it could still contain HTML.
// Due to this, after we strip tags from the string, we should return null if there is nothing left in the resulting string.
if (strLength == 0)
{
return null;
}
// Truncate the text if it is too long.
else if (strLength >= maxLength)
{
fixedString = fixedString.Substring(0, maxLength);
// Unless we take the next step, the string truncation could occur in the middle of a word.
// Using LastIndexOf we can find the last space character in the string and truncate there.
fixedString = fixedString.Substring(0, fixedString.LastIndexOf(" "));
}
fixedString += "...";
return fixedString;
}
// This code sample does not use TwoWay binding, so we do not need to flesh out ConvertBack.
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
通过在“解决方案资源管理器”中双击“App.xaml”,然后在 <Application.Resources> 标记中添加以下代码将 RssTextTrimmer 类设置为转换器:
XAML
<converter:RssTextTrimmer xmlns:converter="clr-namespace:namespace" x:Key="RssTextTrimmer" />
其中,namespace 是项目的命名空间的名称。例如,在 Windows Phone 8 示例的基本 RSS 阅读器中,namespace 被替换为 sdkBasicRSSReaderWP8CS。
更新 XAML 代码
更新 XAML 代码
在“解决方案资源浏览器”中双击 MainPage.xaml 文件。
将 <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid> 替换为下面的 XAML 代码:
XAML
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="0,0,12,0">
<Button Content="Load Feed" Height="72" HorizontalAlignment="Left" Margin="9,6,0,0" Name="loadFeedButton" VerticalAlignment="Top" Width="273" Click="loadFeedButton_Click" />
<ListBox Name="feedListBox" Height="468" HorizontalAlignment="Left" Margin="20,100,0,0" VerticalAlignment="Top" Width="444" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="feedListBox_SelectionChanged">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel VerticalAlignment="Top">
<TextBlock TextDecorations="Underline" FontSize="24" Name="feedTitle" TextWrapping="Wrap" Margin="12,0,0,0" HorizontalAlignment="Left" Foreground="{StaticResource PhoneAccentBrush}" Text="{Binding Title.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedSummary" TextWrapping="Wrap" Margin="12,0,0,0" Text="{Binding Summary.Text, Converter={StaticResource RssTextTrimmer}}" />
<TextBlock Name="feedPubDate" Foreground="{StaticResource PhoneSubtleBrush}" Margin="12,0,0,10" Text="{Binding PublishDate.DateTime}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Border BorderBrush="{StaticResource PhoneSubtleBrush}" BorderThickness="1" Height="2" HorizontalAlignment="Left" Margin="20,88,0,0" Name="border1" VerticalAlignment="Top" Width="438" />
</Grid>
更新代码隐藏文件
更新代码隐藏文件的步骤
添加以下命名空间:
C#
using System.IO;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;
将以下代码添加到 MainPage 类中,放在 MainPage 构造函数后面。
注意:
以下代码中还包括对基本逻辑删除的支持。
C#
// Click handler that runs when the 'Load Feed' or 'Refresh Feed' button is clicked.
private void loadFeedButton_Click(object sender, System.Windows.RoutedEventArgs e)
{
// WebClient is used instead of HttpWebRequest in this code sample because
// the implementation is simpler and easier to use, and we do not need to use
// advanced functionality that HttpWebRequest provides, such as the ability to send headers.
WebClient webClient = new WebClient();
// Subscribe to the DownloadStringCompleted event prior to downloading the RSS feed.
webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(webClient_DownloadStringCompleted);
// Download the RSS feed. DownloadStringAsync was used instead of OpenStreamAsync because we do not need
// to leave a stream open, and we will not need to worry about closing the channel.
webClient.DownloadStringAsync(new System.Uri("http://windowsteamblog.com/windows_phone/b/windowsphone/rss.aspx"));
}
// Event handler which runs after the feed is fully downloaded.
private void webClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
{
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Showing the exact error message is useful for debugging. In a finalized application,
// output a friendly and applicable string to the user instead.
MessageBox.Show(e.Error.Message);
});
}
else
{
// Save the feed into the State property in case the application is tombstoned.
this.State["feed"] = e.Result;
UpdateFeedList(e.Result);
}
}
// This method determines whether the user has navigated to the application after the application was tombstoned.
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
{
// First, check whether the feed is already saved in the page state.
if (this.State.ContainsKey("feed"))
{
// Get the feed again only if the application was tombstoned, which means the ListBox will be empty.
// This is because the OnNavigatedTo method is also called when navigating between pages in your application.
// You would want to rebind only if your application was tombstoned and page state has been lost.
if (feedListBox.Items.Count == 0)
{
UpdateFeedList(State["feed"] as string);
}
}
}
// This method sets up the feed and binds it to our ListBox.
private void UpdateFeedList(string feedXML)
{
// Load the feed into a SyndicationFeed instance.
StringReader stringReader = new StringReader(feedXML);
XmlReader xmlReader = XmlReader.Create(stringReader);
SyndicationFeed feed = SyndicationFeed.Load(xmlReader);
// In Windows Phone OS 7.1 or later versions, WebClient events are raised on the same type of thread they were called upon.
// For example, if WebClient was run on a background thread, the event would be raised on the background thread.
// While WebClient can raise an event on the UI thread if called from the UI thread, a best practice is to always
// use the Dispatcher to update the UI. This keeps the UI thread free from heavy processing.
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
// Bind the list of SyndicationItems to our ListBox.
feedListBox.ItemsSource = feed.Items;
loadFeedButton.Content = "Refresh Feed";
});
}
// The SelectionChanged handler for the feed items
private void feedListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
ListBox listBox = sender as ListBox;
if (listBox != null && listBox.SelectedItem != null)
{
// Get the SyndicationItem that was tapped.
SyndicationItem sItem = (SyndicationItem)listBox.SelectedItem;
// Set up the page navigation only if a link actually exists in the feed item.
if (sItem.Links.Count > 0)
{
// Get the associated URI of the feed item.
Uri uri = sItem.Links.FirstOrDefault().Uri;
// Create a new WebBrowserTask Launcher to navigate to the feed item.
// An alternative solution would be to use a WebBrowser control, but WebBrowserTask is simpler to use.
WebBrowserTask webBrowserTask = new WebBrowserTask();
webBrowserTask.Uri = uri;
webBrowserTask.Show();
}
}
}
快又稳
2024-10-23 广告
2024-10-23 广告
广州快又稳网络科技有限公司是一家集技术研发、产品创新、服务优化于一体的高新技术企业。公司自成立以来,始终秉承“网络使人类缩进距离,我们让网络快又稳”的核心理念,致力于为全球客户提供高效、安全、易用的互联网解决方案及信息技术服务。在数字时代,...
点击进入详情页
本回答由快又稳提供
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询