使用xaml和c#将html语言显示到textblock或richtextblock中

时间:2017-04-05 03:28:30

标签: c# html uwp

我有一个使用HTML语言的文字如下:

<p><span><span>What will complete the trip is buy souvenirs from the visited place to bring them back home. Especially when come in&nbsp;</span><span class="highlight">Surabaya</span><span>&nbsp;the city of heroes where famous for its tourist destinations. This second largest city in Indonesia attracts thousands of people to visit the city during holiday and spend their spare time visiting historic places and enjoying amusements offered by the city.</span><br /><br /><span class="highlight">Surabaya</span><span>&nbsp;is the official capital of the East Java province which has become the Metropolis city with numbers of diversities among the people, cultures, habits and way of living.&nbsp;</span><span class="highlight">Surabaya</span><span>&nbsp;has functioned as an industrial, commercial, maritime, education and government city. This multi-function policy has required&nbsp;</span><span class="highlight">Surabaya</span><span>&nbsp;to provide land for industrial areas and infrastructure support facilities.</span></span></p> <p><span>This second largest city in Indonesia attracts thousands of people to visit the city during holiday and spend their spare time visiting historic places and enjoying amusements offered by the city.&nbsp;To complete trip during in Surabaya, there are four SMEs Centers where providing various kinds of local products. Some of Surabaya SMEs Centers are placed in public shopping center, while the others are placed in SMEs Center Building itself. Visitors do not need to be confused to find a place to buy souvenirs.&nbsp;</span></p> <p><span>Here, easily to find Surabaya souvenirs is something that real. Everyone could see various kinds of handmade product in unique designs. The things&nbsp;</span>such as Fashion, Food and Beverages, Home Decoration, Handicraft, Accessories, Beauty and Body Works product, Furniture, Garment and many more. Surabaya SMEs Center has expected to become &lsquo;one stop shop&rsquo; building for local and international customers who want and need to buy the Indonesia products, especially Surabaya products.</p> <p><span>There are more than 170 SMEs here. The other SMEs Centers in Surabaya are Tunjungan City SMEs Center, Mall ITC SMEs Center, and Mall CITO SMEs Center. You can find many things at Surabaya Souvenir Centers, from the handicrafts, traditional clothing; including many batik collections, shoes, bags; and of course the food. Here, everyone can spoil their eyes of a huge unique souvenirs as far as they can see.</span></p> <p>&nbsp;</p> <p>More info visit: <a title="Surabaya Souvenir Center" href="http://surabaya.indonesia-product.com" target="_blank"><span style="color: #ff0000; font-size: medium;"><em>surabaya.indonesia-product.com</em></span></a></p>"

我想使用XAML和C#将其显示到TextBlock或Richtextbloack中。怎么样?

2 个答案:

答案 0 :(得分:1)

RichTextBlock控件不接受HTML文本和格式的布局。它有自己的格式(如下所示)。但是,您可以编写一个转换后的HTML转换并将其转换为RichTextBlock理解的格式。

<RichTextBlock IsTextSelectionEnabled="True" SelectionHighlightColor="Pink" FontFamily="Arial"  >
    <Paragraph>
        <Run Foreground="Blue" FontWeight="Light" Text="This is some" ></Run>
        <Span FontWeight="SemiBold">
            <Run FontStyle="Italic">sample text to</Run>
            <Run Foreground="Red">demonstrate some properties.</Run>
        </Span>
    </Paragraph>
</RichTextBlock>

如果您确实需要显示HTML,则可能需要将其放入WebBrowser控件中。

<强>更新

有一个NuGet包实现了RichTextBlock的这种转换。在Visual Studio NuGet包管理器中搜索RichTextBlock.Html2Xaml,安装该库,您可以调用Html2Xaml方法为您提供RichTextBlock中可以呈现的内容。

答案 1 :(得分:0)

请注意:https://www.componentone.com/Studio/Input-Editing/RichTextBoxUWP

这是为UWP扩展的RichTextBox。从描述我们可以阅读

Display and edit formatted text as HTML and RTF documents with ComponentOne RichTextBox™如此开心的编码。

编辑;

或尝试在WebView控件上使用WebView.NavigateToString