This topic further explains the presence and purpose of the two XML namespace (xmlns) mappings as found in the root element of most Silverlight XAML files. It also describes how to produce similar mappings for using elements that are defined in your own code, and/or within separate assemblies such as those that are distributed as client libraries in the Silverlight SDK.
This topic contains the following sections.
The Silverlight and XAML Language XAML Namespace Declarations
Within the root element of many XAML files, there are two xmlns declarations. The first declaration maps the Silverlight core XAML namespace as the default:
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Notice that this is the same XAML namespace that WPF uses as its default.
The second declaration maps a separate XAML namespace for the XAML-defined language elements, mapping it (typically) to the x: prefix:
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
This xmlns value is also identical to the one used by WPF.
The relationship between these declarations is that XAML is a language definition, and Silverlight is one implementation that uses XAML as a language and defines a specific vocabulary where its types are referenced in XAML. Silverlight in particular uses a strict subset of XAML language elements. The XAML language specifies certain language elements, and each of these should be accessible through XAML processor implementations working against the XAML namespace. Silverlight also uses a subset of the set of XAML language elements that are implemented in WPF.
The x: mapping convention for the XAML language XAML namespace is followed by project templates, sample code, and the documentation of language features within this documentation set. This XAML namespace defines several commonly used features that are necessary even for basic Silverlight-based applications. For instance, in order to join any code-behind to a XAML file through a partial class, you must name that class as the x:Class attribute in the root element of the relevant XAML file. Or, any element as defined in a XAML page that you wish to access as a keyed resource in a ResourceDictionary must have the x:Key attribute set on the object element in question.
Mapping Prefixes to CLR Namespaces and Assemblies
You can map a XAML namespace to an assembly and to the CLR namespaces within it by using a series of tokens within an xmlns prefix declaration, similar to how the XAML namespace value is mapped to the x: prefix, but by explicitly declaring the assembly and CLR namespace rather than by specifying a URI.
The syntax takes the following possible named tokens and following values:
clr-namespace: The common language runtime (CLR) namespace declared within the assembly that contains the public types to expose to XAML usage.
assembly= The assembly that contains some or all of the referenced CLR namespace.
Note that the character separating the clr-namespace token from its value is a colon (:) whereas the character separating the assembly token from its value is an equals sign (=). The character to use between these two tokens is a semicolon. Do not include whitespace anywhere within the mapping value. For example:
xmlns:custom="clr-namespace:SDKSample;assembly=SDKSampleLibrary"
Mapping to SDK Client Library Assemblies
You can map prefixes to reference the types that are contained in any assembly that is distributed in the Silverlight SDK, as one of the client libraries. In fact you must map a prefix to use XAML for such types, because the types in the library assemblies are not considered to be part of the core Silverlight XAML namespace http://schemas.microsoft.com/winfx/2006/xaml/presentation. DataGrid and Calendar are examples of types that come from client libraries and must be mapped with a prefix in order to be referenced in XAML.
For example, to map a controls prefix that enables you to reference the Calendar class and use it as an object element in XAML, your XAML page should include the following mapping on the root element:
xmlns:controls="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls"
Visual Studio XAML editing includes a feature that presents a dropdown when you define a new xmlns value in XAML that helps simplify the mapping process. For best results, reference the client library assembly from the project first, then map a prefix from it.
Note: |
|---|
Mapping the prefix is not all that is necessary to use classes from the client libraries. You must also distribute or otherwise reference the client library assemblies as part of your application structure and deployment settings. For more information, see Application Structure. |
Mapping to Custom Assemblies
You can map prefixes to reference the types that are contained in any custom assembly that you create and then distribute as part of your Silverlight-based application.
For example, to map a custom1 prefix that enables you to reference a CustomClasses namespace that you define in an assembly named Custom, and use classes from that namespace/assembly as object elements in XAML, your XAML page should include the following mapping on the root element:
xmlns:custom1="clr-namespace:CustomClasses;assembly=Custom"
Mapping to Current Assemblies
assembly can be omitted if the clr-namespace referenced is defined within the same assembly as the application code that references the custom classes (this is generally the EntryPointAssembly). Or, an equivalent syntax for altogether omitting assembly information for mapping the same assembly is to specify assembly=, with no string token following the equals sign.
Partial classes do not need to be mapped; only classes that are not the partial class of a page in your application need to be mapped if you intend to reference them as elements in XAML.
Concepts
Other Resources