1

我正在尝试在 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 ↑&lt;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 ↑&lt;Run>Friedan</Run>, who also helped to start it</Span><LineBreak /></Paragraph></Section>
        </FlowDocument>
    </FlowDocumentScrollViewer>

但是我想从后面的代码中以编程方式执行此操作,但它不起作用。它显示与插入的 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 代码的最佳方法是什么?

4

1 回答 1

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 ↑&lt;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 一样呈现

例如

<章节>...</章节>

而不是您期望的那个。

于 2014-09-18T08:35:29.163 回答