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:
public class App : Application
ContentControl root = new ContentControl();
RootVisual = root;
Button btn = new Button();
btn.Content = "Click me!";
btn.FontSize = 96;
btn.Margin = new Thickness(192);
btn.RenderTransformOrigin = new Point(0.5, 0.5);
root.Content = btn;
RotateTransform rotate = new RotateTransform();
btn.RenderTransform = rotate;
DoubleAnimation anima = new DoubleAnimation();
anima.From = 0;
anima.To = 360;
Storyboard storyboard = new Storyboard();
btn.Click += delegate
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.