2013-02-18 20 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