Using Custom Namespaces at the Root of a XAML File

Here’s something I didn’t know about Silverlight, and I did not find any documentation on it. It’s possible to create custom namespaces at the root of a XAML file. I always knew that you could insert namespaces inline in XAML, but apparently when you define a namespace attribute in XAML it also applies to the root. Here’s a simple example:

<myType:TreeMapPanelItem

x:Class="MIXOnline.Descry.Controls.CustomTreeMapItem"

xmlns=https://schemas.microsoft.com/winfx/2006/xaml/presentation

xmlns:x=https://schemas.microsoft.com/winfx/2006/xaml

xmlns:myType="clr-namespace:MIXOnline.Descry;assembly=Inaugural" >

<TextBlock Text="Hello" />

</myType:TreeMapPanelItem>

This allows replacing of custom elements programmatically as was done in the CPP Custom Treemap sample in Descry which needed to inherit from a base TreeMapPanelItem class to pick up some of its default properties and methods. This could not have been done with a standard Silverlight class since they cannot inherit from custom classes. See the CPP TreeMap sample in Descry for an example and look to the CustomTreeMapItem class for an example of an implementation. Note the assignment of the PanelItemType property in the Page.xaml.cs file which lets the TreeMapItem know which class to use for rendering.