public void WeiboContent ( string weiboContent )
{
if (weiboContent!=String.empty){
string content = weiboContent;
Regex rootRegex = new Regex(@"(((http://)|(ftp://)|(https://)|(zune://)+)[^(\s)]{1,})|(\[{1}\w{0,}[\u4e00-\u9fa5]{0,}\])|(@[^\s]{1,}:)|(#[^\s]{1,}#)");//字符串前加@表示之后的字符串作为正常的字符串解析,不用进行转义
string target = content as string;
target = HttpUtility.HtmlDecode(target);
Paragraph rootParagraph = new Paragraph();
Paragraph imgParagraph = new Paragraph();
MatchCollection mc = rootRegex.Matches(target);
if (mc.Count != 0)
{
int flag = 0;
for (int i = 0; i < mc.Count; i++)
{
if (mc[i].Index != 0)
{
//非匹配内容
Run r = new Run();
r.Text = target.Substring(flag, mc[i].Index - flag);
flag = flag + r.Text.Length;
rootParagraph.Inlines.Add(r);
}
if (mc[i].Value.StartsWith("#"))
{
//#话题#
if (mc[i].Value.Length > 2)
{
Hyperlink h = new Hyperlink();
SolidColorBrush brushTopic = new SolidColorBrush(Colors.Blue);
h.Foreground = brushTopic;
Dictionary<string, string> param = new Dictionary<string, string>();
string text = mc[i].Value.Remove(0, 1);
text = text.Remove(text.Length - 1, 1);
param.Add("query", text);
h.NavigateUri = new Uri("/Detail.xaml?" + param, UriKind.Relative);
Run r = new Run();
r.Text = mc[i].Value;
h.Inlines.Add(r);
rootParagraph.Inlines.Add(h);
flag = flag + mc[i].Value.Length;
}
}
else if (mc[i].Value.StartsWith("["))
{
//[表情]
if (mc[i].Value.Length > 2)
{
Image ImageControl = new Image();
String imageSource = EmotionManager.GetEmotionSource(mc[i].Value); //获取表情uri
ImageControl.Source = new BitmapImage(new Uri(imageSource, UriKind.Relative));
ImageControl.Stretch = Stretch.Fill;
ImageControl.Width = GetContentWidth("中");
ImageControl.Height = GetContentWidth("中");
InlineUIContainer imgUi = new InlineUIContainer();
imgUi.Child = ImageControl;
rootParagraph.Inlines.Add(imgUi);
flag = flag + mc[i].Value.Length;
}
}
else if (mc[i].Value.StartsWith("@"))
{
//@
Dictionary<string, string> param = new Dictionary<string, string>();
param.Add("content", mc[i].Value);
Hyperlink h = new Hyperlink();
SolidColorBrush brushRefer = new SolidColorBrush(Colors.Blue);
h.Foreground = brushRefer;
h.NavigateUri = new Uri("/Detail.xaml?" + param, UriKind.Relative);
Run r = new Run();
r.Text = mc[i].Value;
h.Inlines.Add(r);
rootParagraph.Inlines.Add(h);
flag = flag + mc[i].Length;
}
else
{
//URL
Hyperlink h = new Hyperlink();
h.TargetName = "_blank";
h.NavigateUri = new Uri(mc[i].Value);
Run r = new Run();
r.Text = mc[i].Value;
h.Inlines.Add(r);
rootParagraph.Inlines.Add(h);
flag = flag + mc[i].Length;
}
}
if (flag < target.Length - 1)
{
Run r = new Run();
r.Text = target.Substring(flag, target.Length - flag);
flag = flag + r.Text.Length;
rootParagraph.Inlines.Add(r);
}
}
else
{
rootParagraph.Inlines.Add((new Run()).Text = target);
}
richTextBox.Blocks.Add(rootParagraph);
richTextBox.Blocks.Add(imgParagraph);
SolidColorBrush brush=new SolidColorBrush(Colors.Gray);
richTextBox.Foreground = brush;
}
//获取文本的宽度
private double GetContentWidth(String aSrc)
{
TextBlock text = new TextBlock();
text.FontSize = contentFontSize;
text.Text = aSrc;
return text.ActualWidth;
}
参考原文:http://www.wpdevn.com/showtopic-32.aspx RichTextBox 数据绑定