Windows Phone

Mar 9, 2013 at 1:36 PM
Edited Mar 9, 2013 at 1:36 PM
Would be great if the project would be modified to allow Windows Phone ( it does, but when adding the dll there is a warning that it's native silverlight ).

Also if it could be a Dependency property it would be awesome!
Cfr example: http://code.msdn.microsoft.com/windowsapps/Html-to-RTF-a757fac5#content
Mar 12, 2013 at 11:49 PM
Here you go ;)
public class HTMLToRTFConverter : IValueConverter
{
    private HtmlDocument _htmlDoc;
    private Dictionary<string, IHtmlType> _ruleDic = new Dictionary<string, IHtmlType>();

    public HTMLToRTFConverter()
    {
        this.Init();
    }

    private void AppendHtmlType(IHtmlType hType)
    {
        if (_ruleDic.ContainsKey(hType.TagName))
            _ruleDic[hType.TagName] = hType;
        else
            _ruleDic.Add(hType.TagName, hType);
    }

    private void Init()
    {
        //add tag type
        AppendHtmlType(new HToXaml("h1"));
        AppendHtmlType(new HToXaml("h2"));
        AppendHtmlType(new HToXaml("h3"));
        AppendHtmlType(new HToXaml("h4"));
        AppendHtmlType(new HToXaml("h5"));
        AppendHtmlType(new HToXaml("h6"));
        AppendHtmlType(new HToXaml("h7"));
        AppendHtmlType(new BrToXaml());
        AppendHtmlType(new IToXaml());
        AppendHtmlType(new StrongToXaml("b"));
        AppendHtmlType(new StrongToXaml("strong"));
        AppendHtmlType(new AToXaml());

        //add special type
        AppendHtmlType(new ImageToXaml());
        AppendHtmlType(new ULToXaml());
        AppendHtmlType(new OLToXaml());
        AppendHtmlType(new DLToXaml());
        AppendHtmlType(new BlockQuoteToXaml());
        AppendHtmlType(new TableToXaml());

        //add default type
        AppendHtmlType(new DefaultToXaml());
    }

    private void ConvertHtmlNode(HtmlNode htmlNode, Block block)
    {
        string htmlNodeName = htmlNode.Name.ToLower();

        if (new[] { "p", "div" }.Contains(htmlNodeName))
        {
            foreach (var childHtmlNode in htmlNode.ChildNodes)
            {
                if (string.IsNullOrEmpty(htmlNode.InnerHtml))
                    continue;
                ConvertHtmlNode(childHtmlNode, block);
            }
            var br = new LineBreak();
            (block as Paragraph).Inlines.Add(br);
        }

        if (new[] { "span" }.Contains(htmlNodeName))
        {
            foreach (var childHtmlNode in htmlNode.ChildNodes)
            {
                if (string.IsNullOrEmpty(htmlNode.InnerHtml))
                    continue;
                ConvertHtmlNode(childHtmlNode, block);
            }
        }

        if (_ruleDic.ContainsKey(htmlNodeName))
            _ruleDic[htmlNodeName].ApplyType(htmlNode, block);
    }

    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        if (!ReferenceEquals(value, null) && value is string)
        {
            _htmlDoc = new HtmlDocument();
            _htmlDoc.OptionCheckSyntax = true;
            _htmlDoc.OptionFixNestedTags = false;
            _htmlDoc.OptionAutoCloseOnEnd = true;
            _htmlDoc.LoadHtml((string)value);

            RichTextBox richTextBox = new RichTextBox();
            //richTextBox.Height = 450;
            richTextBox.Foreground = Common.GetColorFromHexa("#027087");

            Paragraph paragraph = new Paragraph();
            foreach (var node in _htmlDoc.DocumentNode.ChildNodes)
                ConvertHtmlNode(node, paragraph);

            //paragraph.Inlines.Add(new Run() { Text = (string)value });
            richTextBox.Blocks.Add(paragraph);

            return richTextBox;
        }

        return null;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}
Jan 28, 2014 at 7:57 AM
Edited Jan 28, 2014 at 7:58 AM