Rose
Rose

Reputation: 611

Display text using html language into textblock or richtextblock using xaml and c#

I have a text using the HTML language as below:

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

I want to show it into a TextBlock or Richtextbloack using XAML and C#. How to?

Upvotes: 1

Views: 2666

Answers (2)

Niewidzialny
Niewidzialny

Reputation: 340

Look at: https://www.componentone.com/Studio/Input-Editing/RichTextBoxUWP

This is extended RichTextBox for UWP. From description we can read

Display and edit formatted text as HTML and RTF documents with ComponentOne RichTextBox™ So happy codding.

edit;

or try use WebView.NavigateToString on WebView control

Upvotes: 0

Pedro Silva
Pedro Silva

Reputation: 717

The RichTextBlock control doesn't accept HTML text and formatting for its layout. It has its own format (shown below). However, you could write a converted that took HTML and converted it to the format that RichTextBlock understands.

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

If you actually need to show HTML, you likely need to put it into the WebBrowser control.

Update:

There's a NuGet package that implements this conversion for RichTextBlock. In your Visual Studio NuGet package manager search for RichTextBlock.Html2Xaml, install that library, and they you can call the Html2Xaml method to provide you with something presentable in the RichTextBlock.

Upvotes: 1

Related Questions