【发布时间】:2015-07-03 02:39:01
【问题描述】:
我想问有没有办法从 Windows Phone 8.1 上的 Rss Reader 中删除 html taq 我正在开发应用程序,该应用程序在智能手机“Windows Phone”上使用 Rss 显示任何网站上的主题但我有这个问题我可以删除 html taqs 这张照片会告诉你我的意思??
【问题讨论】:
标签: c# windows xaml windows-phone-8.1
我想问有没有办法从 Windows Phone 8.1 上的 Rss Reader 中删除 html taq 我正在开发应用程序,该应用程序在智能手机“Windows Phone”上使用 Rss 显示任何网站上的主题但我有这个问题我可以删除 html taqs 这张照片会告诉你我的意思??
【问题讨论】:
标签: c# windows xaml windows-phone-8.1
这是什么控制?您是否考虑过使用 Web 浏览器控件?或者,如果您确实需要在 TextBox 中显示它(假设它是 TextBox),您可以使用 tutorial 将您的 HTML 转换为 XAML FlowDocument。
【讨论】:
我遵循相同的教程来创建我的 rss 阅读器。 因此,要删除 html 代码,您需要创建一个类来修剪描述: 使用代码创建一个名为 RssTextTrimmer.cs 的类:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Globalization;
using System.Text.RegularExpressions;
using Windows.UI.Xaml.Data;
using System.Net;
namespace VEJA_Notícias
{
public class RssTextTrimmer : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, string language)
{
if (value == null) return null;
int maxLength = 200;
int strLength = 0;
string fixedString = "";
// Remove HTML tags.
fixedString = Regex.Replace(value.ToString(), "<[^>]+>", string.Empty);
// Remove newline characters.
fixedString = fixedString.Replace("\r", "").Replace("\n", "");
// Remove encoded HTML characters.
fixedString = WebUtility.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;
}
public object ConvertBack(object value, Type targetType, object parameter, string language)
{
throw new NotImplementedException();
}
}
}
您需要在 MainPage.xaml 中添加代码:
<Page.Resources>
<local:RssTextTrimmer x:Key="RssTextTrimmer" />
</Page.Resources>
要访问 xaml 文本块中 RssTextTrimmer 类的方法,您需要这样做:
<TextBlock x:Name="textTitulo" Text="{Binding title, Converter={StaticResource RssTextTrimmer}}"/>
【讨论】: