From 6a92a6a4d27dc07f8b32bd7d57ffbcbe15630ab9 Mon Sep 17 00:00:00 2001 From: Vitaly Takmazov Date: Sun, 12 Feb 2012 01:53:36 +0400 Subject: Avatars support + broken clickable urls --- Juick/Classes/RichTextConverter.cs | 58 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 Juick/Classes/RichTextConverter.cs (limited to 'Juick/Classes/RichTextConverter.cs') diff --git a/Juick/Classes/RichTextConverter.cs b/Juick/Classes/RichTextConverter.cs new file mode 100644 index 0000000..ee6ffa7 --- /dev/null +++ b/Juick/Classes/RichTextConverter.cs @@ -0,0 +1,58 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Net; +using System.Windows; +using System.Windows.Controls; +using System.Windows.Data; +using System.Windows.Documents; +using System.Windows.Ink; +using System.Windows.Input; +using System.Windows.Media; +using System.Windows.Media.Animation; +using System.Windows.Shapes; +using Microsoft.Phone.Tasks; + +namespace Juick.Classes +{ + public class RichTextConverter : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + var message = value as string; + var runs = new List(); + + foreach (var word in message.Split(new char[] {' ', '\n'})) + { + Uri uri; + + if (Uri.TryCreate(word, UriKind.Absolute, out uri) || + (word.StartsWith("www.") && Uri.TryCreate("http://" + word, UriKind.Absolute, out uri))) + { + var link = new Hyperlink(); + link.Inlines.Add(new Run() { Text = word }); + link.Click += (sender, e) => + { + var hyperLink = (sender as Hyperlink); + new WebBrowserTask() { Uri = uri }.Show(); + }; + + runs.Add(link); + } + else + { + runs.Add(new Run() { Text = word }); + } + + runs.Add(new Run() { Text = " " }); + } + + return runs; + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + return value; + } + } +} -- cgit v1.2.3