5

Silverlight5アプリケーションに非常に単純なRichTextBoxコントロールがあります。

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

データが変更されると、ローカル変数に次のようにキャプチャします。

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

すべての作品。しかし、ほんの少しの単語を入力して<Paragraph>マークアップを調べると、それは巨大です!スニペットは以下のとおりです。マークアップにすべてのタイポグラフィのものを含めない方法はありますか?なぜそこにあり、必要なのですか?それを削除する方法はありますか?

<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>
4

1 に答える 1

4

私は他の人を助けるために私の解決策を共有すると思いました...それは数ヶ月間実施されており、悪い副作用は見られませんでした。

タイポグラフィ属性の削除を支援するために、いくつかの拡張メソッドを作成しました。

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を再レンダリングするときに、問題は発生していません。このソリューションが誰かに役立つことを願っています!

于 2012-09-04T13:20:40.057 に答える