Charles Petzold

Experimental Embedding of Silverlight Apps in Blog Entries

December 15, 2009
New York, N.Y.

Commonly, blog entries that discuss Silverlight programming contain a link to a separate HTML or ASPX file that contains the actual Silverlight application. I want to start embedding Silverlight applications right in my blog entries. Since I wrote my own blogging software — it's a Windows Forms application called BlogHack and it's as sloppy as the name suggests — I figured it wouldn't be too difficult.

And it wasn't. BlogHack lets me enter and edit blog entries and maintains a database called blogbase.xml. When updating the blog, BlogHack generates several files that it copies to my web site: The blog.xml and rss.xml files are very similar; they share a number of tags and contain the most recent entries. A blogformat.xsl file formats the blog.xml file for viewing. The "archive" consists of a toc.html file ("toc" stands for "table of contents") and an HTML file for each blog entry. Each month's worth of blog entries goes in a separate directory.

I recently altered BlogHack so that only the first paragraph of each blog entry was copied into blog.xml and rss.xml, so I knew I didn't have to worry about those. That reduced the problem to getting the Silverlight into the individual HTML files. In theory, it's only a matter of embedding an <object> tag, but I wanted to bring the JavaScript in as well for the Silverlight installation.

Whenever there's a blog entry with an embedded Silverlight application, the directory for that month of blog entries will contain the standard Silverlight.js file as well as a ClientBin directory containing all the Silverlight XAP files for that month.

And here we are. On the left is a little bitty Silverlight program that displays a ScrollBar and sends the value locally using the LocalMessageSender class. On the right is another tiny Silverlight program with a TextBlock and an event handler for the MessageReceived event of the LocalMessageReceiver class. Manipulate the ScrollBar and the TextBlock changes.