MakubeX

导航

Silverlight,WP7读取rss...

需要用到一个据说是silverlight3的 dll文件.叫 System.ServiceModel.Syndication.dll 

下面是代码...

 private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
#region
var backgroundBrush = this.Resources["PhoneBackgroundBrush"] as SolidColorBrush;
if (backgroundBrush.Color == Colors.White)
{
// Light Theme
}
else
{
// Dark Theme
}
#endregion

var rss = RssResources.cd;
using (var strm = new System.IO.StringReader(rss))
using (var reader = System.Xml.XmlReader.Create(strm))
{
var feed = System.ServiceModel.Syndication.SyndicationFeed.Load(reader);

var tagFinder = new Regex("<(.|\n)+?>", System.Text.RegularExpressions.RegexOptions.IgnoreCase);
string str = string.Empty;

foreach (var item in feed.Items)
{
var txt = new TextBlock() { TextWrapping = TextWrapping.Wrap };
this.PostStack.Children.Add(txt);

txt.Inlines.Add(new Run()
{
FontWeight = FontWeights.Bold,
Text = item.Title.Text,
FontSize = txt.FontSize * 2
});
txt.Inlines.Add(new LineBreak());
var itemText = item.Summary.Text;

var match = tagFinder.Match(itemText);
int startidx = 0, isBold = 0, isItalics = 0;
bool isHyperlink = false;

while (match.Index >= 0 && match.Length > 0)
{
if (isHyperlink)
{
Hyperlink(itemText, str, startidx, match.Index, tagFinder);
}
else
{
ExtractText(txt, itemText, startidx, match.Index, tagFinder, isBold > 0, isItalics > 0);
}
startidx = match.Index + match.Length;

var isEndTag = match.Value.Contains("/");

if (match.Value == "</p>" || match.Value == "<p />" || match.Value == "<br />")
{
// Found the end of a paragraph, so add line break
txt.Inlines.Add(new LineBreak());
}
else if (match.Value == "<b>" || match.Value == "</b>")
{
isBold += isEndTag ? -1 : 1;
}
else if (match.Value == "<i>" || match.Value == "</i>" || match.Value == "<em>" || match.Value == "</em>")
{
isItalics += isEndTag ? -1 : 1;
}
else if (match.Value.Contains("<a"))
{
isHyperlink = true;
str = itemText.Substring(match.Index, startidx - match.Index);
}
else if (match.Value.Contains("/a>"))
{
isHyperlink = false;
}
else if (match.Value.Contains("<img"))
{
// Locate the url of the image
var idx = match.Value.IndexOf("src");
var url = match.Value.Substring(idx + 5, match.Value.IndexOf("\"", idx + 6) - (idx + 5));

// Create an image and add to stackpanel
var img = new Image()
{
Source = new BitmapImage(new Uri(url))
};
img.Width = this.PostStack.ActualWidth / 2;
img.Stretch = Stretch.UniformToFill;
this.PostStack.Children.Add(img);

// Create a new textblock and add to stackpanel
txt = new TextBlock() { TextWrapping = TextWrapping.Wrap };
this.PostStack.Children.Add(txt);
}

// Look for the next tag
match = tagFinder.Match(itemText, match.Index + 1);

}
// Add the remaining text
txt.Inlines.Add(itemText.Substring(startidx));

// Add some space before the next post
txt.Inlines.Add(new LineBreak());
txt.Inlines.Add(new LineBreak());
}
}
}

private static void ExtractText(TextBlock txt, string source, int startidx, int endidx, Regex tagFinder, bool isBold, bool isItalics)
{
var text = source.Substring(startidx, endidx - startidx);
text = tagFinder.Replace(text, "");
if (!isItalics && !isBold)
{
txt.Inlines.Add(text);
}
else
{
txt.Inlines.Add(new Run()
{
FontWeight = (isBold ? FontWeights.Bold : FontWeights.Normal),
FontStyle = (isItalics ? FontStyles.Italic : FontStyles.Normal),
Text = text
});
}
}

 

需要添加一个 Resource1.resx 资源文件. 把xml 放到这里....  这里xml文件的名字叫做 cd

posted on 2012-02-18 10:28  Makubex  阅读(272)  评论(0编辑  收藏  举报