【问题标题】:Excessive XAML markup size in Silverlight RichTextBoxSilverlight RichTextBox 中的 XAML 标记大小过大
【发布时间】:2012-05-13 02:10:59
【问题描述】:

我在 silverlight 5 应用程序中有一个非常简单的 RichTextBox 控件。

<RichTextBox x:Name="rtbControl" 
    Height="Auto" 
    ContentChanged="rtbControl_ContentChanged" />

随着数据的变化,我将其捕获在一个局部变量中:

    private void rtbControl_ContentChanged(object sender, ContentChangedEventArgs e)
    {
        RichTextContent = rtbControl.Xaml;
    }

所有作品。但是,如果我只输入几个字并检查 &lt;Paragraph&gt; 标记,那真是太棒了!下面是一个sn-p。有没有办法在标记中不包含所有排版内容?为什么它存在并且需要它?有没有办法去掉?

<Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<Paragraph FontSize="11" FontFamily="Segoe UI" Foreground="#FF000000" FontWeight="Normal" FontStyle="Normal" FontStretch="Normal" 
CharacterSpacing="0" Typography.AnnotationAlternates="0" Typography.EastAsianExpertForms="False" Typography.EastAsianLanguage="Normal" 
Typography.EastAsianWidths="Normal" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" 
Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" 
Typography.ContextualAlternates="True" Typography.StylisticAlternates="0" Typography.StylisticSet1="False" Typography.StylisticSet2="False"
 Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" 
Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" 
Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" 
Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" 
Typography.StylisticSet20="False" Typography.Capitals="Normal" Typography.CapitalSpacing="False" Typography.Kerning="True" Typography.CaseSensitiveForms="False" 
Typography.HistoricalForms="False" Typography.Fraction="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.SlashedZero="False"
 Typography.MathematicalGreek="False" Typography.Variants="Normal" TextOptions.TextHintingMode="Fixed" TextOptions.TextFormattingMode="Ideal" 
TextOptions.TextRenderingMode="Auto" TextAlignment="Left" LineHeight="0" LineStackingStrategy="MaxHeight"><Run>was it a </Run><Run TextDecorations="Underline">bad 
</Run><Run>blah? or </Run></Paragraph></section>

【问题讨论】:

  • 哇,这太棒了 :( 我认为需要持久性以确保您获得准确的格式。
  • 你想做什么?也许您可以使用文本或选定文本属性?请参阅此msdn.microsoft.com/fr-fr/library/ff426926%28v=vs.95%29.aspx 官方示例。
  • 我让用户能够创建文档片段,我需要为他们提供富文本格式功能。用户输入被推送到数据库。我确实需要捕捉他们所做的任何格式。现在的方式是,用户可以在 3 行上输入 4 个单词,并且其文本已经超过 4000 个字符。
  • 嗯,我看到了两种减小文件大小的方法:压缩和解析 XML 以从 Paragraph & co 中删除所有默认属性。
  • 但是我没有看到内置的解决方案,抱歉。

标签: silverlight richtextbox silverlight-5.0


【解决方案1】:

我想我会分享我的解决方案来帮助他人……它已经使用了几个月,我还没有看到任何不良的副作用。

我创建了一些扩展方法来帮助删除 Typography 属性。

public static class RichTextBoxExtensions {
        // <summary>
        /// Removes any xml tag with the prefix given from a string
        /// </summary>
        /// <param name="XMLString"></param>
        /// <param name="TagPrefix"></param>
        /// <returns></returns>
        public static string RemoveXMLAttributesFromNode(this string XMLString, string prefix)
        {
            //Match [Prefix][any number of Not =][=]["][any number of Not "]["][ ] <-must have space!!!
            string replace = prefix + "[^\\=]*=\"[^\"]*\" ";
            return Regex.Replace(XMLString, replace, string.Empty);
        }
        /// <summary>
        /// Removes any xml tag with prefixed by an element in unWanted
        /// </summary>
        /// <param name="XMLString"></param>
        /// <param name="TagPrefix"></param>
        /// <returns></returns>
        public static string RemoveXMLAttributesFromNode(this string XMLString, List<string> unWanted)
        {

            foreach (string prefix in unWanted)
            {
                //Match [Prefix][any number of Not =][=]["][any number of Not "]["][ ] <-must have space!!!
                string replace = prefix + "[^\\=]*=\"[^\"]*\" ";
                XMLString = Regex.Replace(XMLString, replace, string.Empty);
            }
            return XMLString;
        }

接下来,在我将字符串保存到数据库之前,我删除了错误的属性:

        List<string> badAttributes = new List<string>();
        badAttributes.Add("Typography");
        var ffText = ffi.FreeFormText == null ? null : ffi.FreeFormText.RemoveXMLAttributesFromNode(badAttributes);

删除这些属性后重新渲染 RTF 时,我没有发现任何问题。希望这个解决方案可以帮助某人!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-05
    • 1970-01-01
    相关资源
    最近更新 更多