我有Silverlight应用程序将RichTextBox保存在XAML中,如下所示:
<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);
}
错误:
Primeraexpepcióndeltipo'System.Windows.Markup.XamlParseException'en 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)”的最佳重载方法匹配具有一些无效参数
请帮助我,我需要找到一种方法来读取在本地WPF应用程序中在Sirvelight中创建的XAML字符串。
答案 0 :(得分:0)
首先
http://www.schemas.microsoft.com/winfx/2006/xaml/presentation
应该是
假设这是一个拼写错误,您的下一个问题是使用XAML Parser。即使在WPF中存在Comentario,Section和Paragraph,它们也可以存在于不同的命名空间中,并且可能具有不同的属性。
鉴于你已经辞职将XAML转换为FlowDocument,也许最好跳过XAML解析器。
为什么不使用XDocument代替XamlReader?