Understanding XAML Namespace in Silverlight


As a high storey building cannot be built on a loose foundation, similarly a rich Silverlight application cannot be made without solid foundation and clear understanding of XAML.

Extensible Application Markup Language (XAML) is a markup language used to instantiate .NET objects. Although XAML is a technology that can be applied to many different problem domains, it was initially designed as a part of Windows Presentation Foundation (WPF).

Conceptually, XAML plays a role that's a lot like HTML, and is even closer to its stricter cousin, XHTML. XHTML allows you to define the elements that make up an ordinary web page. Similarly, XAML allows you to define the elements that make up a XAML content region.

XAML Namespace:

Before continuing, take a look at this bare-bones XAML document, which represents a blank page (as created by Visual Studio). The lines have been numbered for easy reference:

 <UserControl x:Class="SilverlightApplication1.MainPage"
 mc:Ignorable="d" d:DesignWidth="640" d:DesignHeight="480">

 <Grid x:Name="LayoutRoot">

Let's start observing each line.

  1. <UserControl>: When you use an element like <UserControl> in a XAML file, the Silverlight parser recognizes that you want to create an instance of the UserControl class. However, it doesn't necessarily know what UserControl class to use. After all, even if the Silverlight namespaces only include a single class with that name, there's no guarantee that you won't create a similarly named class of your own. Clearly, you need a way to indicate the Silverlight namespace information in order to use an element. There are four namespaces by default

    xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation: Is the core Silverlight namespace. It encompasses all the essential Silverlight classes, including the UserControl and Grid.

    xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml : It includes various XAML utility features that allow you to influence how your document is interpreted. This namespace is mapped to the prefix x. That means you can apply it by placing the namespace prefix before the name of an XML element or attribute (as in <x:ElementName> and x:Class="ClassName").

    xmlns:d="http://schemas.microsoft.com/expression/blend/2008": is a namespace reserved for design-specific XAML features that are supported in Expression Blend (and now Visual Studio 2010). It's used primarily to set the size of the design surface for a page. The DesignWidth and DesignHeight properties are a part of it.

    xmlns:mc=http://schemas.openxmlformats.org/markup-compatibility/2006 :  the XAML compatibility namespace. You can use it to tell the XAML parser what information must to process and what information to ignore.

  2. XMLS: The xmlns attribute is a specialized attribute in the world of XML and it's reserved for declaring namespaces.

  3. The namespace information allows the XAML parser to find the right class. For example, when it looks at the UserControl and Grid elements, it sees that they are placed in the default http://schemas.microsoft.com/winfx/2006/xaml/presentation namespace. It then searches the corresponding Silverlight namespaces, until it finds the matching classes System.Windows.UserControl and System.Windows.Controls.Grid.

  4. The XML namespace name doesn't correspond to a single Silverlight namespace. Instead, all the Silverlight namespaces share the same XML namespace.

  5. XML namespaces are often URIs. These URIs look like they point to a location on the Web, but they don't. The URI format is used because it makes it unlikely that different organizations will inadvertently create different XML-based languages with the same namespace. Because the domain schemas.microsoft.com is owned by Microsoft, only Microsoft will use it in an XML namespace name.

Custom Namespace:

So far we have seen the default namespaces provided by Silverlight, but we can also create custom namespaces. In many situations, we'll want to have access to your own namespaces in a XAML file. So the most common example is if we want to use a custom Silverlight control that you (or another developer) have created. In this case, you need to define a new XML namespace prefix and map it to your assembly. Here's the syntax you need:

<UserControl x:Class="SilverlightApplication1.MainPage"
xmlns:w="clr-namespace:Nayan;assembly=VishalLibrary" ... >

The XML namespace declaration sets three pieces of information:

  • The XML namespace prefix: You'll use the namespace prefix to refer to the namespace in
    your XAML page. In this example, that's w, although you can choose anything you want that doesn't conflict with another namespace prefix.

  • The .NET namespace: In this case, the classes are located in the Nayan namespace. If
    you have classes that you want to use in multiple namespaces, you can map them to different XML namespaces or to the same XML namespace (as long as there aren't any conflicting class names).

  • The assembly: In this case, the classes are part of the VishalLibrary.dll assembly. (You don't include the .dll extension when naming the assembly.) Silverlight will look for that assembly in the same XAP package where your project assembly is placed.

If you want to use a custom control that's located in the current application, you can omit the assembly part of the namespace mapping, as shown here:


Once you've mapped your .NET namespace to an XML namespace, you can use it anywhere in your XAML document. For example, if the Widgets namespace contains a control named NayanHotButton, you could create an instance like this:

<w: NayanHotButton, Text="Click Me!" Click="DoSomething"></w: NayanHotButton,>

I hope it did some needful in understading fundamentals about XAML namespaces.


Similar Articles