繁体   English   中英

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

[英]FlowDocument doesn't show my XAML code from code behind and instead shows it as HTML tags

我正在尝试在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>

当我将我的XAML代码插入flowdocument标记内时,它可以完美显示内容并设置格式:

<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>

但是我想从后面的代码中进行此编程,但这是行不通的。 它显示未格式化的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代码的最佳方法是什么?

您可能需要将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一样呈现

例如

<节> ... </节>

而不是您期望的那种。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM