Built-in Types for Common XAML Language Primitives
XAML 2009 introduces XAML language-level support for several data types that are frequently used primitives in the common language runtime (CLR) and in other programming languages. XAML 2009 adds support for these primitives: x:Object, x:Boolean, x:Char, x:String, x:Decimal, x:Single, x:Double, x:Int16, x:Int32, x:Int64, x:TimeSpan, x:Uri, x:Byte, and x:Array
This topic contains the following sections.
- Previous Techniques for Language Primitives in XAML Markup
- XAML 2009 Language Primitives
- WPF Support
In XAML for previous WPF versions, you could reference the CLR language primitives by mapping the assembly and namespace that contained a CLR primitive definition class for the .NET Framework. Most of these are in the mscorlib assembly and System namespace. For example, to use Int32, you could declare the following mapping (with an example usage shown thereafter):
<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sys="clr-namespace:System;assembly=mscorlib"> <Application.Resources> <sys:Int32 x:Key="intMeaning">42</sys:Int32> </Application.Resources> </Application>
By convention, the language primitives for XAML and all other XAML language elements are shown, including the x: prefix. This is how XAML language elements are typically used in real-world markup. This convention is followed in the conceptual documentation for XAML in WPF and also in the XAML specification.
For CLR backing, the x:Object primitive corresponds to Object.
This primitive is not typically used in application markup, but might be useful for some scenarios such as checking assignability in a XAML type system.
For CLR backing, the x:Decimal primitive corresponds to Decimal.
Note that XAML parsing is inherently done under en-US culture. Under en-US culture, the correct separator for the components of a decimal is always a period (.) regardless of culture settings of the development environment, or of the eventual client target where the XAML is loaded at run time.
For the XAML language specification definition, see [MS-XAML] Sections 5.2.14 and 5.4.8.
For CLR backing, the x:Single primitive corresponds to Single.
In addition to the numeric values, text syntax for x:Single also permits the tokens Infinity, -Infinity, and NaN. These tokens are treated as case sensitive.
x:Single can support values in scientific notation form, if the first character in text syntax is e or E.
For the XAML language specification definition, see [MS-XAML] Sections 5.2.8 and 5.4.2.
For CLR backing, the x:Double primitive corresponds to Double.
In addition to the numeric values, text syntax for x:Double permits the tokens Infinity, -Infinity, and NaN. These tokens are treated as case sensitive.
x:Double can support values in scientific notation form. Use the character e or E to introduce the exponent portion.
For the XAML language specification definition, see [MS-XAML] Sections 5.2.9 and 5.4.3.
For CLR backing, the x:Array primitive corresponds to Array.
You can define an array in XAML 2006 by using a markup extension syntax; however, the XAML 2009 syntax is a language-defined primitive that does not require accessing a markup extension. For more information about XAML 2006 support, see x:Array Markup Extension.
For the XAML language specification definition, see [MS-XAML] Sections 5.2.18.
In WPF, you can use XAML 2009 features but only for XAML that is not markup-compiled. Markup-compiled XAML for WPF and the BAML form of XAML do not currently support the XAML 2009 keywords and features.
A scenario where you can use XAML 2009 features together with WPF is if you author loose XAML and you then load that XAML into a WPF runtime and object graph with XamlReader.Load. The WPF System.Windows.Markup.XamlReader and its Load can process XAML 2009 language keywords and features into a valid object graph representation.