タイムラインの文字列表示処理

Twitterのタイムラインの内容をRichTextBoxやTextBlockで表示する(リンクの付加など)ためのコード
(CoreTweet使用)

Twitterのタイムラインの内容をRichTextBoxやTextBlockで表示する(リンクの付加など)ためのコード
(CoreTweet使用)

public static void TwitterObectToInline(InlineCollection inlines, CoreTweet.Core.CoreBase obj, bool clearInlines = false)
{
if (obj == null || obj == null) return;
//
if (clearInlines) inlines.Clear();
string text = null;
IList<Entity> dic = null;
//
if (obj is Status)
{
var status = (Status)obj;
text = status.Text;
dic = ConcatArray<Entity>( //
status.Entities.UserMentions,
status.Entities.Urls,
status.Entities.Symbols,
status.Entities.Media,
status.Entities.HashTags);
}
else if (obj is DirectMessage)
{
var message = (DirectMessage)obj;
text = message.Text;
dic = ConcatArray<Entity>(
message.Entities.UserMentions,
message.Entities.Urls,
message.Entities.Symbols,
message.Entities.Media,
message.Entities.HashTags);
}
if (dic == null) return;
if (dic.Count > 0)
{
//
//
dic = dic.OrderBy(entity => entity.Indices[0]).ToList();
// 0
if (dic[0].Indices[0] != 0)
inlines.Add(HttpUtility.HtmlDecode(text.Substring(0, dic[0].Indices[0])));
//
dic.ForEach((entity, index) =>
{
// *** ***
var link = new Hyperlink()
{
Cursor = Cursors.Hand,
};
if (entity is UserMentionEntity)
{
var user = (UserMentionEntity)entity;
link.Inlines.Add(string.Format("@{0}", user.ScreenName));
}
else if (entity is UrlEntity)
{
var url = (UrlEntity)entity;
link.Inlines.Add(url.DisplayUrl);
}
else if (entity is SymbolEntity)
{
var symbol = (SymbolEntity)entity;
link.Inlines.Add(string.Format("{0}{1}",
text[symbol.Indices[0]] == '$' ? '$' : '#',
symbol.Text));
}
else if (entity is MediaEntity)
{
var media = (MediaEntity)obj;
link.Inlines.Add(media.MediaUrl.AbsoluteUri);
}
inlines.Add(link);
// *** / ***
var len = entity.Indices[1]; //
if (len != text.Length)
{
if (dic.Count > index + 1)
inlines.Add(HttpUtility.HtmlDecode(
text.Substring(len, dic[index + 1].Indices[0] - len)));
else
inlines.Add(HttpUtility.HtmlDecode(
text.Substring(len, text.Length - len)));
}
});
}
else inlines.Add(HttpUtility.HtmlDecode(text));
dic.Clear();
dic = null;
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX