The page has been moved to GitHub

HtmlTextBlock

Package: MyToolkit.Extended
Platforms: WP7SL, WP8SL, WinRT(Universal) (only FixedHtmlTextBlock without integrated ScrollViewer)

With this control you can display html without using the WebBrowser control. The control uses an extendable parser which generates native controls.

The following generators are implemented:
  • Italic and bold font (em and strong tag)
  • Image (img tag)
  • Link (a tag)
  • Paragraph (p tag)
  • Unordered lists (ul and li tags)
The HtmlTextBlock has scrollbars by default to support lazy loading of objects (virtualizing list). You can use the HeaderTemplate property to add an generic header on top of the generated controls within the scroll viewer. There is also a FixedHtmlTextBlock class without scrollbars (but which is not virtualized list - worse performance).

<controls:HtmlTextBlock ShowHeader="True" ShowFooter="True" Html="My html" x:Name="HtmlPresenter">
	<controls:HtmlTextBlock.HeaderTemplate>
		<DataTemplate>
			<TextBlock Text="My header" />
		</DataTemplate>
	</controls:HtmlTextBlock.HeaderTemplate>
	<controls:HtmlTextBlock.FooterTemplate>
		<DataTemplate>
			<TextBlock Text="My footer" />
		</DataTemplate>
	</controls:HtmlTextBlock.FooterTemplate>
</controls:HtmlTextBlock>

Changing styles

The generated HTML elements can be enhanced or changed by registering to the Loaded event of the HtmlTextBlock element and changing the dictionary in the Generators property:

private void OnHtmlTextBlockLoaded(object sender, RoutedEventArgs e)
{
    var block = (HtmlTextBlock)sender;
    block.Generators["h1"] = new ParagraphGenerator
    {
        FontSize = (double)Resources["TextStyleLargeFontSize"] + 6, 
        FontWeight = FontWeights.Bold
    };
    block.Generators["h2"] = new ParagraphGenerator
    {
        FontSize = (double)Resources["TextStyleLargeFontSize"] + 4, 
        FontWeight = FontWeights.Bold
    };
    block.Generators["h3"] = new ParagraphGenerator
    {
        FontSize = (double)Resources["TextStyleLargeFontSize"] + 2,
        FontWeight = FontWeights.Bold
    };
}

It is also possible to implement new generators for new HTML tags.

Performance

You should always prefer to use HtmlTextBlock with HeaderTemplate and FooterTemplate than a FixedHtmlTextBlock in a ScrollViewer with own elements as header and footer because the first variant provides list virtualization and has much better performance for big html content.

Notes

  • Some generators are missing in the WinRT version.


Last edited Jun 3, 2015 at 10:58 PM by rsuter, version 25