A in XAML: Ain’t for WPF/Silverlight

Even though XAML is officially the acronym of eXtensible Application Markup Language,- a generic term-, you can not usually separate it from UI frameworks, WPF and Silverlight. This creates some confusion, most people think of WPF as the declaration of UI with XML. Not that this is wrong, this explanation does not necessarily suggest decoupling data, logic and user interaction. XAML only helps WPF to achieve it, there is definitely more in WPF. I admit, XAML originally stood for eXtensible Avalon (code-name for WPF) Markup Language. But hey, there is more in XAML, too.

Since the first day I learned about WPF, I almost never used a UI designer tool. Perhaps, that is why I started using XAML everywhere I could, one could say it was love at first sight. They say love lasts roughly 3 years and even though it has been a little bit longer than that for me, I still feel strong about it. Lucky for me, Microsoft does also. Together with the recent release of VS2010 Beta 2, XAML finally has its own assembly, System.Xaml.dll targeting .NET Framework 4 Beta 2. I have now plenty of projects which can loose their dependencies on PresentationCore.dll, PresentationFramework.dll and WindowsBase.dll. I hope to convert them all easily and start making more.

First one to go was my almost forgotten open-source project Geostructor. I was expecting minor problems but I was wrong. I had none except the fact that, I had unconsciously created dependencies on WPF assemblies. After moving those dependent codes to where they really belong, I was ready to go with one little replacement. Replaced all class references of XamlReader and XamlWriter with the new XamlServices class. Not only everything worked as expected, some of my problems were gone too. This is what made me to write about the problem.

Back when I first encountered this problem, I was planning to post my solution here. Then, I heard about a new feature in the upcoming System.Xaml.dll and thought I should wait for it. In other words, I was lazy. So here it goes:

Old Problem

Here is a sample file saved by Geostructor v0.3, which targets .NET Framework 3.5 SP1:

Here, RelatedPoint property of RelativePoint is of type IGPoint which inherits INotifyPropertyChanged interface. RelatedPoint objects listen to the PropertyChanged events of the referenced objects. In order to keep these references after saving and loading back, I had to prevent XamlWriter from producing such result:

Old Solution


First, I tried to extend the XamlReader and XamlWriter by providing custom services. However, I remember I was blocked by various finalized classes. The solution I came up with was to mark reference properties with attributes, converting these references to unique strings (by taking their Name property). Later, during XamlReader, again I had to convert these string values into place-holder objects. Remember, converters had no way of knowing the current context. When XamlReader is done and I have the collection, I had to go through the elements and replace the place-holders:

No Problem

Here is the new saved file in Geostructor v0.4, which targets .NET Framework 4.0:

In order to achieve this, you have to do nothing, thanks to Reference Markup Extension which actually utilize the IXamlNameResolver Interface. It has been a long wait since Rob announced this feature. Not that I am complaining, now I have to go through the code and remove the unnecessary attributes and converters. Also I should probably investigate possible usage of this new service in WPFix library.

Hopefully, I will keep on posting more about what I have to remove from my code instead of adding. BTW, to most, this is probably one of the least awaited feature in VS2010, and they are right. Go check out the Beta 2, it is much better than Beta 1.

One thought on “A in XAML: Ain’t for WPF/Silverlight”

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">