PETZOLD BOOK BLOG

Charles Petzold on writing books, reading books, and exercising the internal UTM


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

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:

NoXamlApp.html

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.


Comments:

This touches on an area I have been pondering alot lately. I am in the midst of writing an exporter from 3DSMax to WPF (Viewport3D, etc...). I started writing it so it exported proper XAML, and then it occured to me it that I could just output a generic database that got parsed as I loaded an app and instantiate Media3D objects at runtime. I decided to keep going with XAML since that seemed to be the main way of doing things. Plus it had the side benefit of the display parts loading, displaying, and animating all by themselves if I just click on the XAML file.

Then I hit textures. And there appears to be no way to embed a texture map in a XAML file. I was able to find some samples that show how to do this with cooresponding C# code to unpack it.

In the end I wonder what the most efficient way to do this is. For now I am sticking with XAML. Since I am writting games in WPF it is convient to be able to reference a scene/gameplay element by name in C#. Plus, it is hard to imagine calling oneself a WPF expert and not being intimitely familiar with authiring via XAML.

Finally, I APPRECIATE YOUR BLOG!! Please keep it up!!

Best regards.

starman2049, Mon, 23 Mar 2009 11:39:35 -0400 (EDT)

Now that's a happy XAML.

Deirdre, Mon, 23 Mar 2009 13:17:23 -0400 (EDT)

Great to see you are moving toward stripping away the complexity of Silverlight applications.

I hope you keep blogging about Silverlight in the near future..

— Anthony, Mon, 23 Mar 2009 13:25:28 -0400 (EDT)

Charles,

I see that XamlReader.Load is supported in Silverlight 2, do you know if Silverlight supports grabbing XAML from a resource or may be even off the net?

— Anthony, Tue, 24 Mar 2009 16:55:10 -0400 (EDT)

I haven't tried storing XAML as app resources, but you can definitely load XAML files stored on the server relative to ClientBin using WebClient. I haven't tried it, but according to this

you should be able to load XAML files from anywhere. — Charles

Wow you could probably even make a XAML Cruncher for Silverlight! That would be cool!

— Anthony, Tue, 24 Mar 2009 17:03:42 -0400 (EDT)

Yep: XamlCruncher for Silverlight — Charles

I never thought about this. Very interesting!

Christian Wirth, Tue, 23 Jun 2009 10:35:47 -0400 (EDT)

The less Xaml the better.

— Sim99, Sat, 22 Aug 2009 07:19:43 -0400 (EDT)


Recent Entries
< PreviousBrowse the ArchivesNext >
Subscribe to the RSS Feed

(c) Copyright Charles Petzold
www.charlespetzold.com