Welcome to MSDN Blogs Sign in | Join | Help

Bringing a bit of HTML to Silverlight [HtmlTextBlock makes rich text display easy!]

Lately I've seen a few people wanting to display rich text in a Silverlight application, but having no way to do so easily. Most recently, I saw Tim Heuer bump into this when displaying RSS content in a neat demo of his. The basic problem is that Silverlight's primary text display object, TextBlock, does not natively have a way to display HTML - and that's the format most rich text is in these days. It seemed that if only there were a TextBlock that took HTML as input and built up a collection of Run and LineBreak objects corresponding to that HTML, things would be easier...

So I wrote HtmlTextBlock, a "plug-compatible" replacement for TextBlock that knows how to take simple HTML (technically XHTML, see the notes below) and display it in a manner that fairly closely approximates how a web browser does. A picture is worth a thousand words, so here's what HtmlTextBlock looks like in action:

HtmlTextBlock Demonstration Page

You can click here (or on the image above) to experiment with HtmlTextBlock in your own browser in an interactive demo page. As usual, I've made the complete source code available, so click here to download the source code and play around with it yourself! (To build the project, you'll want to use Visual Studio 2008 Beta 2 and the latest Silverlight Tools.)

Notes:

  • HtmlTextBlock supports the following HTML elements: <A>, <B>, <BR>, <EM>, <I>, <P>, <STRONG>, and <U>. Attributes are not supported with the exception of the <A> element's HREF attribute (see below).
  • I initially planned to have HtmlTextBlock derive from TextBlock and simply override its Text property. However, TextBlock is sealed, so that wasn't going to work. The next obvious approach was to have HtmlTextBlock be a Control with a TextBlock inside it, so that's what I've done here. My goal of "plug-compatibility" (i.e., the ability to easily replace TextBlock with HtmlTextBlock) meant that I needed to manually implement the various TextBlock properties on HtmlTextBlock and pass them through to the underlying TextBlock. So HtmlTextBlock doesn't actually derive from TextBlock, but it behaves as though it did.
  • Having HtmlTextBlock use TextBlock internally and represent the HTML markup with Run and LineBreak elements is nice because it means that just about everything people already know about TextBlock automatically applies to HtmlTextBlock And I get correct word wrapping behavior for free. :) However, the glitch is that the Run element does not support the MouseLeftButton* family of events, making the handling of <A> link elements difficult. Because HtmlTextBlock can't really tell when the user clicks on a link, it displays the URL of each link so the user can type it in themselves (and typing is necessary because TextBlock doesn't support select+copy either). I can think of a few reasons why Run might not support mouse events, but in this case it would be convenient if it did. :)
  • HtmlTextBlock uses an XmlReader to parse its input when creating the Run and LineBreak elements for display. This makes the parsing implementation simple and robust, but breaks down when the input is not XHTML (such as invalid HTML or valid HTML where empty elements don't have a trailing '/' (ex: "<br>" instead of "<br />")). In the event of a parsing error, HtmlTextBlock's default behavior is to display the input text as-is, just like TextBlock always does. Unfortunately, non-XHTML is pretty common, so certain scenarios may benefit from a custom HTML parser implementation that is more flexible in this regard.
  • HTML rendering is covered by a detailed specification, but some of the rules are not what one might expect. Specifically, the rules for handling spacing and whitespace (' ', '\n', '\t', etc.) can be tricky to get right. I've made some attempt to ensure that HtmlTextBlock follows the rules where possible and convenient, but it was not my goal to achieve 100% compliance in this area. Playing around in the demo application, HtmlTextBlock should pretty closely match the browser's HTML rendering of valid input, but if you know the rules, it's not too difficult to trigger whitespace rendering differences.
  • Silverlight's default font, "Portable User Interface"/"Lucida Sans Unicode"/"Lucida Grande" doesn't seem to render bold or italic text any differently than normal text. This always surprises people the first time they try to figure out why their text isn't bold/italic. I don't know why this is myself, but I'd guess it relates to keeping the download size of Silverlight to a minimum.
  • The demo page makes use of JavaScript -> C# function calls. Hooking this up in code is surprisingly easy, though I did run into a small glitch. If the sample text is deleted entirely (leaving a blank text box) in IE, the JS -> C# call originates from the JS side as it should, but never makes it through to the C# side. Looking at the call to SetText in the debugger, textAreaSampleText.value is "", so this should work in IE just like it does in Firefox - but it didn't for me. The simple workaround was to pass textAreaSampleText.value + "" instead and then things worked in IE, too.

HtmlTextBlock is obviously nothing like a complete HTML rendering engine [that's what web browsers are for! :) ]. However, if you want to add simple support for rich text to your Silverlight application without a lot of work, HtmlTextBlock may be just the thing for you!

Published Monday, September 10, 2007 12:53 PM by Delay
Filed under:

Attachment(s): SilverlightHtmlTextBlock.png

Comments

# Silverlight Cream for September 11, 2007

Tuesday, September 11, 2007 9:38 AM by WynApse

Silverlight Cream for September 11, 2007

# HTML Textblock for Silverlight

Tuesday, September 11, 2007 2:00 PM by Eric Griffin's Blog

Need to display rich text in Silverlight? Go here to check out the demo here . click here to download

# HtmlTextBlock for Silverlight

Tuesday, September 11, 2007 9:01 PM by POKE 53280,0: Pete Brown's Blog

This is a great control. We could have used the ability to display HTML text in the Carbon Calculator,

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Sunday, September 16, 2007 8:40 PM by ScottGu's Blog

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Sunday, September 16, 2007 9:03 PM by BusinessRx Reading List

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Sunday, September 16, 2007 9:07 PM by Elan's Aggregated Blogs

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Sunday, September 16, 2007 9:21 PM by ASP.NET

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Tuesday, September 18, 2007 11:23 AM by Programming

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# Thursday Linkorama

Thursday, September 20, 2007 3:09 AM by Ross Hawkins

Thursday Linkorama

# Bringing more HTML to Silverlight [HtmlTextBlock improvements]

Monday, September 24, 2007 2:17 PM by Delay's Blog

I blogged about my HtmlTextBlock implementation for Silverlight a few days ago. In that post I described

# Silverlight Cream for September 25, 2007

Tuesday, September 25, 2007 2:31 PM by WynApse

Silverlight Cream for September 25, 2007

# Expression Blend 2 September Preview

Wednesday, September 26, 2007 9:57 AM by Mind....it

Expression Blend 2 September Preview

# September 16th Links: ASP.NET, ASP.NET AJAX, IIS7, Visual Studio, Silverlight

Wednesday, October 03, 2007 11:01 PM by ASPInsiders

Here is the latest in my link-listing series . Also check out my ASP.NET Tips, Tricks and Tutorials page

# Continuing support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 1!]

Monday, March 17, 2008 3:48 PM by Delay's Blog

A few months ago when Silverlight 1.1 Alpha was all the rage, I wrote a sample control that made a best-effort

# Afficher du texte enrichi avec Silverlight 2

Monday, April 21, 2008 10:57 AM by Christophe Lauer, Blog Edition

Si vous avez un peu jou&#233; avec la version B&#234;ta 1 de Silverlight 2 vous avez certainement remarqu&#233;

# Again with the support for simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 Beta 2!]

Wednesday, June 11, 2008 1:15 PM by Delay's Blog

A customer recently asked about an update to my HtmlTextBlock sample for the newly released Silverlight

# HtmlTextBlock for Silverlight

Saturday, June 28, 2008 11:46 PM by DEVELOPMENT SITE - NOT MY PUBLIC BLOG

This is a great control. We could have used the ability to display HTML text in the Carbon Calculator, and I&#39;ve run into the limitation in other projects. If you want to display HTML rich text in your Silverlight application, and don&#39;t feel like

# My take on simple HTML display in Silverlight [HtmlTextBlock sample updated for Silverlight 2 RTW!]

Wednesday, November 12, 2008 1:14 AM by Delay's Blog

A couple of readers have asked about an update to my long-running HtmlTextBlock sample for Silverlight

# A fix for simple HTML display in Silverlight [HtmlTextBlock bug fix for Silverlight 2 RTW!]

Wednesday, November 12, 2008 3:16 PM by Delay's Blog

I updated my HtmlTextBlock sample for RTW last night and got an email from kind reader Ed Silverton this

# Using HyperLink instead of Run for active links

Wednesday, February 11, 2009 1:48 PM by tevya

I got down the source and was trying to see if I could add HyperLink objects instead of Run objects to the text block.  MSDN documents the HyperLink class but it does not seem to actually exist (in VS 2K8, .NET 3.5).  What's up with that?  Was HyperLink withdrawn or just silently never implemented?

# re: Bringing a bit of HTML to Silverlight [HtmlTextBlock makes rich text display easy!]

Wednesday, February 11, 2009 1:56 PM by Delay

tevya,

System.Windows.Documents.Hyperlink is available in the PresentationFramework assembly of WPF. It's not present in Silverlight because Silverlight doesn't yet support the Documents namespace, etc.. But Silverlight has HyperlinkButton which can be useful for some related scenarios - though not what you're trying to do here, if memory serves.

Hope this helps!

Anonymous comments are disabled
 
Page view tracker