1

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?

Rose
  • 613
  • 4
  • 22
  • So you want to show this as a raw html with tags? – mindOfAi Apr 05 '17 at 03:32
  • No, i want not display the text without the tag. I want display the text with the function of the tag, for example: for new line, for paragraph, etc. And i want if the link. I would like if the text of the link or email, then the clickable text – Rose Apr 05 '17 at 04:05
  • If you are open to use Third party controls, You should definitely look at [ScrollableHTMLView]( https://github.com/MyToolkit/MyToolkit/wiki/ScrollableHtmlView) – AVK Apr 05 '17 at 15:33

2 Answers2

1

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.

Pedro Silva
  • 687
  • 6
  • 14
  • I need a text that can display html content. Is it possible to use a textblock? Or use what? – Rose Apr 05 '17 at 04:45
  • Check out my update about a Nuget package that can convert the html - RichTextBlock.Html2Xaml. – Pedro Silva Apr 05 '17 at 04:54
  • pedro: do you have the sample for that? – Rose Apr 05 '17 at 05:20
  • @Niewidzialny: The sample can not be run on Windows RT or Windows 10 – Rose Apr 05 '17 at 09:52
  • @Rose http://stackoverflow.com/questions/13729540/convert-simple-html-to-a-richtextblock – Niewidzialny Apr 05 '17 at 11:45
  • @Rose, did you get this working with the examples or do you still need sample code? – Pedro Silva Apr 05 '17 at 15:32
  • @PedroSilva I need a sample that can be run on Windows 10, not in wpf – Rose Apr 06 '17 at 02:04
  • @Niewidzialny The code on these links in wpf, whereas I needed on windows 10. Code that is used between wpf and windows 10 (UWP) several different and so can not be used – Rose Apr 06 '17 at 02:05
  • http://stackoverflow.com/questions/35626840/loading-html-file-in-webview-in-xaml-in-uwp-from-app-data-local-folder You can temporary write data to local state folder, load data, and after showing delete data. (data is a .html page) – Niewidzialny Apr 06 '17 at 18:00
  • @Rose i forget add @ – Niewidzialny Apr 06 '17 at 18:50
  • @Niewidzialny i mean not html file, but data from json use html text. i need to display the text on textblock or rich text block on windows 10 use c# and xaml. For example is the html text on the above. I need to show the text without displaying the tag, but the tag function. For example: for the new line, etc. – Rose Apr 07 '17 at 01:33
  • @Rose So, take this data from json. Then save this data as .html file in application cache folder, then take its file path and put this in WebView control in UWP. It will show result of tag. I mean, if something you will see **something**. You have to load if by file because WebView control are loading only by source. So you **have to** create a temporary file. – Niewidzialny Apr 07 '17 at 06:12
  • Also check documentation of RichTextBlock: https://learn.microsoft.com/en-us/windows/uwp/controls-and-patterns/rich-text-block – Niewidzialny Apr 07 '17 at 06:23
0

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

Niewidzialny
  • 330
  • 2
  • 18