Home > Articles > Programming > C#

Mastering XAML

  • Print
  • + Share This
This chapter is from the book


The most mysterious part about comparing the previous XAML examples with the equivalent C# examples is how the XML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation maps to the Windows Runtime namespace Windows.UI.Xaml.Controls. It turns out that the mapping to this and other namespaces is hard-coded inside the framework. (In case you’re wondering, no web page exists at the schemas.microsoft.com URL—it’s just an arbitrary string like any namespace.) Because many Windows Runtime namespaces are mapped to the same XML namespace, the framework designers took care not to introduce two classes with the same name, despite the fact that the classes are in separate Windows Runtime namespaces.

The root object element in a XAML file must specify at least one XML namespace that is used to qualify itself and any child elements. You can declare additional XML namespaces (on the root or on children), but each one must be given a distinct prefix to be used on any identifiers from that namespace. MainPage.xaml in the preceding chapter contains the XML namespaces listed in Table 2.1.

Table 2.1. The XML Namespaces in Chapter 1’s MainPage.xaml


Typical Prefix




The standard UI namespace.xaml/presentation Contains elements such Grid,Button, and TextBlock.



The XAML language namespace. Contains keywords such as Class, Name, and Key.



This using:XXX syntax is the way to use any custom Windows Runtime or .NET namespace in a XAML file. Inthis case, BlankApp is the.NET namespace generated for the project in Chapter 1 because the project itself was named "BlankApp."

http://schemas.microsoft.com/expression/ blend/2008


A namespace for design-time information that helps tools like Blend and Visual Studio show a proper preview.



A markup compatibility namespace that can be used to mark other namespaces/elements as ignorable. Normally used with the design-time namespace, whose attributes should be ignored at runtime.

The first two namespaces are almost always used in any XAML file. The second one (with the x prefix) is the XAML language namespace, which defines some special directives for the XAML parser. These directives often appear as attributes to XML elements, so they look like properties of the host element but actually are not. For a list of XAML keywords, see the “XAML Keywords” section later in this chapter.

Using the UI XML namespace (http://schemas.microsoft.com/winfx/2006/xaml/presentation) as a default namespace and the XAML language namespace (http://schemas.microsoft.com/winfx/2006/xaml) as a secondary namespace with the prefix x is just a convention, just like it’s a convention to begin a C# file with a using System; directive. You could declare a Button in XAML as follows, and it would be equivalent to the Button defined previously:


Of course, for readability it makes sense for your most commonly used namespace (also known as the primary XML namespace) to be prefix free and to use short prefixes for any additional namespaces.

The last two namespaces in Table 2.1, which are plopped in pages generated by Visual Studio and Blend, are usually not needed.

  • + Share This
  • 🔖 Save To Your Account