在WPF TextBlock中显示XAML格式的文本

时间:2014-09-17 20:45:57

标签: c# html wpf xaml textblock

这是HTML字符串的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>

我将HTML转换为XAML,因为TextBlock不支持要显示的html标记。 WebBrowser不是我想要的

如何在格式化版本的TextBlock中显示此XAML代码?

1 个答案:

答案 0 :(得分:2)

TextBlock仅支持Inlines,因此Sections和Paragraphs是上述xaml中不支持的段落

这是一个不包括相同

的例子
<TextBlock xml:space="preserve">
    <Hyperlink NavigateUri="E6A88D2B.js">click for E6A88D2B.js</Hyperlink>
    <LineBreak />
    <LineBreak />
       <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 />
</TextBlock>

我还为超链接添加了一些文本以使其可见

其他选项包括使用文档查看器

例如

<FlowDocumentScrollViewer>
    <FlowDocumentScrollViewer.Document>
        <FlowDocument>
            <Section xml:space="preserve"
                     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
               <Paragraph>
                   <Hyperlink NavigateUri="E6A88D2B.js">click for E6A88D2B.js</Hyperlink>
               </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.Document>
</FlowDocumentScrollViewer>