Charles Petzold

Silverlight sans Xaml

March 23, 2009
New York, N.Y.

When exploring new programming environments, sometimes it's instructive to strip down an application to determine the minimum code required for the app to run. For example, does a Silverlight application require XAML files? Does a Silverlight application require a class named Page that derives from UserControl? Does a Silverlight application require a class named App that derives from Application?

The answers are No, No, and Yes. The NoXamlApp project (source code here) is an attempt to reduce a Silverlight application to its bare essentials. (But I didn't extend this exercise into the realm of the web-hosting project except for the routine deletion of the aspx files and renaming the test-page html file. Nor did I try to eliminate the AssemblyInfo.cs file.) The NoXamlApp project has no Page.xaml, no Page.xaml.cs, and no App.xaml. The entire App.cs file looks like this:

Of course, this could be considerably shorter, but I wanted to implement something non-trivial — in this case, a button that spins in a circle when it's clicked. You can run it here:


Notice that everything is done in the App constructor, and that I use a ContentControl rather than the customary UserControl so I wouldn't have to derive a new class just to set the Content property.

If you try renaming the App class to something else, it no longer works. Apparently the Application derivative must be named App, and the RootVisual property must be set to some UIElement. But the ContentControl that I use isn't really required, either. You can set RootVisual directly to the Button object, and it'll look a little different but it'll still run.