FlowDocument不会从后面的代码中显示我的XAML代码,而是将其显示为HTML标记

时间:2014-09-18 08:20:27

标签: c# wpf xaml flowdocument flowdocumentscrollviewer

我试图在Flowdocument中显示这个XAML部分

<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代码的最佳方法是什么?

1 个答案:

答案 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="&lt;Section&gt;...&lt;/Section&gt;" />
            </Paragraph>
        </Section>
    </FlowDocument>

这可能会像你看到的html那样渲染

例如

<强>&LT;节&gt; ...&LT; /节&gt;

而不是您期望的那个。