【问题标题】:FlowDocument doesn't show my XAML code from code behind and instead shows it as HTML tagsFlowDocument 不会从后面的代码中显示我的 XAML 代码,而是将其显示为 HTML 标记
【发布时间】:2014-09-18 08:20:27
【问题描述】:

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

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

【问题讨论】:

    标签: c# wpf xaml flowdocument flowdocumentscrollviewer


    【解决方案1】:

    您可能需要将 xaml 解析为适当的对象,而不是在 Run 中插入与字符串值相同的对象。

    XamlReader.Parse 帮助您解析此类字符串并为其初始化/创建对象。

        Section section = XamlReader.Parse(myXamlCode) as Section;
        myFlowDocument.Blocks.Add(section);
    

    上面的示例假设字符串myXamlCode 带有以下文本(如问题所述)

    &lt;Section xml:space='preserve' xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'&gt;&lt;Paragraph&gt;&lt;Hyperlink NavigateUri='E6A88D2B.js'/&gt;&lt;/Paragraph&gt;&lt;Paragraph /&gt;&lt;Paragraph&gt;&lt;Span Foreground='blue'&gt;&lt;Run FontWeight='bold'&gt;NOW, the&lt;/Run&gt;&lt;/Span&gt;&lt;Span&gt;/ˌen əʊ ˈdʌb&lt;Run FontStyle='italic'&gt;ə&lt;/Run&gt;ljuː $ -oʊ-/ &lt;/Span&gt;&lt;Run&gt;BrE&lt;/Run&gt;&lt;Run /&gt;&lt;Run /&gt;&lt;Run&gt;AmE&lt;/Run&gt;&lt;Run /&gt;&lt;Run /&gt;&lt;LineBreak /&gt;&lt;Span&gt;&lt;Span FontWeight='bold'&gt;&lt;Run Foreground='blue'&gt;(the National Organization for Women)&lt;/Run&gt;&lt;/Span&gt; 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&gt;Friedan&lt;/Run&gt;, who also helped to start it&lt;/Span&gt;&lt;LineBreak /&gt;&lt;/Paragraph&gt;&lt;/Section&gt;


    作为旁注,有问题的代码转换为以下内容

        <FlowDocument>
            <Section>
                <Paragraph>
                    <Run Text="&lt;Section&gt;...&lt;/Section&gt;" />
                </Paragraph>
            </Section>
        </FlowDocument>
    

    这可能会像您看到的 html 一样呈现

    例如

    ...

    而不是你所期望的。

    【讨论】:

      猜你喜欢
      • 2014-02-05
      • 2013-03-20
      • 1970-01-01
      • 2020-01-09
      • 2013-01-27
      • 2018-03-11
      • 2020-02-25
      • 2014-08-30
      • 1970-01-01
      相关资源
      最近更新 更多