Not The Smallest Silverlight App
This blog post is not about the Smallest Silverlight App, it is about a Smaller Silverlight App. Mike Taulty (Microsoft) wrote a blog post “World’s Smallest Silverlight App 😉”.
I find this type of articles very informative since they point out essentials. The essential Silverlight application that Mike Taulty comes up with consists of a xaml file and an html file, the html file providing the context in which the Silverlight web browser plug-in renders the xaml.
<Canvas xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"> <TextBlock Text="Hello, I'm Silverlight" FontSize="36"/> </Canvas>
<object data="data:application/x-silverlight-2," type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/splash.xaml" /> </object>
But this is not the smallest application possible. For one thing, the xaml presented here uses the Canvas element as its root element. However, in Silverlight 4 you can use any rendered object (any object that derives from the UIElement class). So we set the TextBlock element as the root element, giving:
<TextBlock xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" Text="Hello, I'm Silverlight" FontSize="36"/>
In my software development environment, this is a single line of code. We should not expect to be able to reduce this much further.
The html, however, is open to further reduction. First of all, let’s remove the comma at the end of the data attribute. What is it doing there? Then note that we have both a data attribute and a param sub-element that refers to the data to be loaded. It turns out; we can safely remove the data attribute, but need the “source” parameter.
<object type="application/x-silverlight-2" width="100%" height="100%"> <param name="source" value="ClientBin/splash.xaml" /> </object>
This is the final proper result, for me. I’m certain there has to be an easy way to inline the xaml in the html. The standard way is to embed the xaml in script, which would increase the size of the code again. I don’t know how to do that, so this is it for me. The above result is Not The Smallest Silverlight App.
However, a not so proper further reduction of the html can be made by using an iframe, like this:
<iframe src="ClientBin/splash.xaml" width="100%" height="100%">
Very short, but it does introduce in the browser an extra, and non-functional, navigation bar at the top of the iframe. At the time of writing this, I haven’t succeeded yet in making it disappear. So, finally, in short and not so proper: The minimum Silverlight application consists of two lines of code. One line of xaml, and one line of html.