<Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>
当我在flowdocument标记内插入我的XAML代码时,它会完美地显示内容并格式化:
<FlowDocumentScrollViewer Width="400" VerticalAlignment="Bottom" Height="200" >
<FlowDocument>
<Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>
</FlowDocument>
</FlowDocumentScrollViewer>
但我想从代码背后做这个programaticaly,它不起作用。 它显示未格式化的XAML文本,与插入的XAML代码
完全相同Paragraph paragraph = new Paragraph();
paragraph.Inlines.Add(new Run(myXamlCode));
Section section = new Section();
section.Blocks.Add(paragraph);
myFlowDocument.Blocks.Add(section);
显示我的XAML代码的最佳方法是什么?
答案 0 :(得分:1)
您可能需要将xaml解析为适当的对象,而不是在Run中插入相同的字符串值。
XamlReader.Parse可帮助您解析此类字符串并初始化/创建对象。
Section section = XamlReader.Parse(myXamlCode) as Section;
myFlowDocument.Blocks.Add(section);
以上示例假设字符串myXamlCode
具有以下文本(如上所述)
<Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'><Paragraph><Hyperlink NavigateUri='E6A88D2B.js'/></Paragraph><Paragraph /><Paragraph><Span Foreground='blue'><Run FontWeight='bold'>NOW, the</Run></Span><Span>/ˌen əʊ ˈdʌb<Run FontStyle='italic'>ə</Run>ljuː $ -oʊ-/ </Span><Run>BrE</Run><Run /><Run /><Run>AmE</Run><Run /><Run /><LineBreak /><Span><Span FontWeight='bold'><Run Foreground='blue'>(the National Organization for Women)</Run></Span> a large US organization started in 1966, which works for legal, economic, and social equality between women and men. Its first president was Betty ↑<Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>
作为附注,有问题的代码转换为以下内容
<FlowDocument>
<Section>
<Paragraph>
<Run Text="<Section>...</Section>" />
</Paragraph>
</Section>
</FlowDocument>
这可能会像你看到的html那样渲染
例如
<强>&LT;节&gt; ...&LT; /节&gt; 强>
而不是您期望的那个。