Charles Petzold



Accessing Non-XAML Assemblies in XAML

February 23, 2006
NYC

The February CTP of WinFx introduces new XAML namespace names. They no longer mention "Avalon," so we know the Avalon folks at Microsoft are getting serious about releasing this baby. Here are the new ones as they will commonly appear in XAML files:

Another change is the elimination of a XML processing instruction named Mapping. Prior to the February CTP, if you wanted to reference elements from non-XAML assemblies (such as your own source code), you had to include a processing instruction that mapped the namespace you defined in your source code to an XML namespace:

and then you had to define a prefix for that XML namespace:

Beginning with the February CTP all the information is consolidated into the XML namespace declaration and you don't have to come up with a separate XML namespace name:

I like this! (A list of breaking changes in the February CTP such as these is available from Karsten Januszewski's blog.)

If you want to use a class in a non-XAML DLL, an extended syntax lets you specify the assembly. Here's how you define a prefix of 's' to use classes and structures defined in the System namespace and the mscorlib.dll assembly:

Notice that a colon separates "clr-namespace" from the CLR namespace, but an equal sign separates "assembly" from the assembly name. It seems inconsistent to me, but I trust that the people who come up with this stuff know what they're doing. After all, they work at Microsoft.

The following piece of XAML uses this new namespace declaration to associate a prefix of 'g' with the System.Globalization namespace in the ever-popular mscorlib.dll assembly. It then uses binding to fill a ListBox with the names of the days of the week, and another binding to display the selected item. These name days are culture specific. If you go into Control Panel and run Regional and Language Options, and switch to French, for example, the ListBox will display dimanche, etc.

You can download or run DaysOfWeek.xaml or gaze upon it here:

Notice the 'g' in front of the DateTimeFormatInfo class.