0

我有将 RichTextBox 保存在 XAML 中的 Silverlight 应用程序,如下所示:

  <Comentario>
  <Section xml:space="preserve" HasTrailingParagraphBreakOnPaste="False" xmlns="http://www.schemas.microsoft.com/winfx/2006/xaml/presentation">
  <Paragraph FontSize="22" FontFamily="Arial" 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 FontSize="22" FontFamily="Janda Apple Cobbler" Foreground="#FF000000">My TEXT</Run>
  </Paragraph>
  </Section>
  </Comentario>

我还有一个必须读取 XAML 的本地 WPF 应用程序。WPF 中的richtextbox 不支持 XAML,所以我必须将此 XAML 转换为 FlowDocument。我尝试了很多方法,但我也得到一个错误:

代码 1:

        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        Section sec = XamlReader.Load(xmlReader) as Section;
        FlowDocument doc = new FlowDocument();
        while (sec.Blocks.Count > 0)
        {
            var block = sec.Blocks.FirstBlock;
            sec.Blocks.Remove(block);
            doc.Blocks.Add(block);
        }

错误:

Primera excepción del Tipo 'System.Windows.Markup.XamlParseException' en PresentationFramework.dll

信息 adicional:'无法创建未知类型'{ http://www.schemas.microsoft.com/winfx/2006/xaml/presentation }Section'。行号“1”和行位置“2”。

代码 2:使用 ParserContext

        System.Windows.Markup.ParserContext parserContext = new System.Windows.Markup.ParserContext();
        parserContext.XmlnsDictionary.Add("", "http://schemas.microsoft.com/winfx/2006/xaml/presentation");
        parserContext.XmlnsDictionary.Add("x", "http://schemas.microsoft.com/winfx/2006/xaml");
        StringReader stringReader = new StringReader(xamlString);
        System.Xml.XmlReader xmlReader = System.Xml.XmlReader.Create(stringReader);
        Section sec = XamlReader.Load(xmlReader,parserContext) as Section;
        FlowDocument doc = new FlowDocument();
        while (sec.Blocks.Count > 0)
        {
            var block = sec.Blocks.FirstBlock;
            sec.Blocks.Remove(block);
            doc.Blocks.Add(block);
        }

错误:错误 14 'System.Windows.Markup.XamlReader.Load(System.IO.Stream, System.Windows.Markup.ParserContext)' 的最佳重载方法匹配有一些无效参数

请帮助我,我需要找到一种方法来读取在我的本地 WPF 应用程序中的 Sirvelight 中创建的 XAML 字符串。

4

1 回答 1

0

第一的

http://www.schemas.microsoft.com/winfx/2006/xaml/presentation

应该

http://schemas.microsoft.com/winfx/2006/xaml/presentation

假设这是一个错字,您的下一个问题是使用 XAML 解析器。即使 WPF 中存在注释、部分和段落,它们也可能存在于不同的命名空间中,并且可能具有不同的属性。

鉴于您已经准备好将 XAML 转换为 FlowDocument,也许最好跳过 XAML 解析器。

为什么不直接使用XDocument而不是 XamlReader?

于 2013-02-19T07:24:29.630 回答