简体   繁体   English

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

[英]Showing XAML formatted text in WPF TextBlock

This is the XAML version of a HTML string 这是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>

I converted HTML to XAML because TextBlock doesn't support html tags to show. 我将HTML转换为XAML,因为TextBlock不支持显示html标记。 And WebBrowser is not what I want WebBrowser不是我想要的

How could I show this XAML code in a TextBlock in formatted version? 如何在格式化的TextBlock中显示此XAML代码?

TextBlock supports only Inlines so Sections and Paragraphs are the one which are not supported in the mentioned xaml TextBlock仅支持内联,因此在上述xaml中不支持Sections和Paragraphs

here is an example excluding the same 这是一个排除相同的例子

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

I also added some text for hyperlink to make it visible 我还为超链接添加了一些文本以使其可见

other options includes using a document viewer 其他选项包括使用文档查看器

example

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

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

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