2013-02-18 70 views
0

我的Silverlight应用程序,保存在XAML RichTextBox的,像这样的:读RichTextBox的XAML Silverlight的WPF中

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

错误:

西甲excepción德尔TIPO“System.Windows.Markup.XamlParseException恩PresentationFramework.dll

Informaciónadicional:'无法创建未知类型'{http://www.schemas.microsoft.com/winfx/2006/xaml/presentation}部分'。'行号“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)”有一些无效参数

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

回答

0

首先

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

应该

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

假设是一个错字,你的下一个问题是使用XAML分析器。即使Comentario,Section和Paragraph存在于WPF中,它们也可以存在于不同的命名空间中,并且可能具有不同的属性。

鉴于您已经辞职将XAML转换为FlowDocument,可能最好跳过XAML解析器。为什么不使用XDocument代替XamlReader?

+0

嗨触发器。感谢您的回答。我仔细检查了我的silverlight代码,并且它有时会保存与http://www.schemas.microsoft.com/winfx/2006/xaml/presentation以及其他情况下的XAML http://schemas.microsoft.com/winfx/2006/XAML /演示。我删除www。从XAML字符串和“作品”,现在我移动到另一个错误,CharacterSpacing不是WPF中的属性,但是Silverlight中的一个属性(它不是这种情况的一部分)。最后一个问题:你知道如何防止Silverlight保存有时与www的XAML。和其他没有它的情况? – 2013-02-19 23:28:29

+0

我希望您经常会遇到不存在的属性,或者在转换中使用不同的名称。正如我之前所说的,如果将XAML字符串视为XML,则不必担心命名空间名称或属性名称,只需将一个Silverlight类型(用XML定义)映射为一个WPF类型即可。 – 2013-02-20 07:20:16