简体   繁体   中英

Read richtextbox XAML from Silverlight in WPF

I have Silverlight application that saves the RichTextBox in XAML, like this:

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

I also have a local WPF application that must read the XAML. The richtextbox in WPF dont support XAML, so i have to convert this XAML to a FlowDocument. I have tried many ways of doing it, but i also get an error:

Code 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);
        }

Error:

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

Información adicional: 'Cannot create unknown type '{ http://www.schemas.microsoft.com/winfx/2006/xaml/presentation }Section'.' Line number '1' and line position '2'.

Code 2: Using 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);
        }

Error: Error 14 The best overloaded method match for 'System.Windows.Markup.XamlReader.Load(System.IO.Stream, System.Windows.Markup.ParserContext)' has some invalid arguments

Please help me, I need to find a way to read the XAML string created in Sirvelight in my local WPF application.

First

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

should be

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

Assuming that is a typo, your next problem is using the XAML Parser. Even if Comentario, Section and Paragraph exist in WPF, they could live in a different namespace and likely have different properties.

Given that you are resigned to converting the XAML to a FlowDocument, perhaps it would be best to skip the XAML parser.

Why not just use XDocument instead instead of XamlReader?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM