Esempio n. 1
0
        private static void HtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is RichTextBlock)
            {
                string html = e.NewValue as string;

                RichTextBlock richText = d as RichTextBlock;
                if (richText == null)
                {
                    return;
                }

                richText.Blocks.Clear();

                StringBuilder sb = new StringBuilder();
                sb.AppendLine("<?xml version=\"1.0\"?>");
                sb.AppendLine("<RichTextBlock xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\" xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">");
                sb.AppendLine(Html2XamlConverter.Convert2Xaml(html, TagAttributes));
                sb.AppendLine("</RichTextBlock>");

                RichTextBlock newRichText = (RichTextBlock)XamlReader.Load(sb.ToString());

                for (int i = newRichText.Blocks.Count - 1; i >= 0; i--)
                {
                    Block b = newRichText.Blocks[i];
                    newRichText.Blocks.RemoveAt(i);
                    richText.Blocks.Insert(0, b);
                }
            }
        }
        private static void HtmlChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            RichTextBlock richText = d as RichTextBlock;
            string        html     = e.NewValue as string;

            if (richText != null && !string.IsNullOrEmpty(html))
            {
                try
                {
                    ChangeRichTextBlockContents(richText, Html2XamlConverter.ConvertToXaml(html));
                }
                catch (Exception ex)
                {
                    AppLogs.WriteError("Html2Xaml.HtmlChanged", ex);
                    try
                    {
                        ChangeRichTextBlockContents(richText, GetErrorXaml(ex, html));
                    }
                    catch
                    {
                        AppLogs.WriteError("Html2Xaml.HtmlChanged", ex);
                    }
                }
            }
        }