
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.