Редактиране

Споделяне чрез


x:TypeArguments Directive

Passes constraining type arguments of a generic to the constructor of the generic type.

XAML Attribute Usage

<object x:TypeArguments="typeString" .../>

XAML Values

Value Description
object An object element declaration of a XAML type, which is backed by a CLR generic type. If object refers to a XAML type that is not from the default XAML namespace, object requires a prefix to indicate the XAML namespace where object exists.
typeString A string that declares one or more XAML type names as strings, which supplies the type arguments for the CLR generic type. See Remarks for additional syntax notes.

Remarks

In most cases, the XAML types that are used as an information item in a typeString string are prefixed. Typical types of CLR generic constraints (for example, Int32 and String) come from CLR base class libraries. Those libraries are not mapped to typical framework-specific default XAML namespaces, and therefore, require a prefix mapping for XAML usage.

You can specify more than one XAML type name by using a comma delimiter.

If the generic constraints themselves use generic types, the nested constraint type arguments can be contained by parentheses ().

Note that this definition of x:TypeArguments is specific to .NET XAML Services and using CLR backing. A language-level definition can be found in [MS-XAML] Section 7.3.11.

Usage Examples

For these examples, assume that the following XAML namespace definitions are declared:

xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=mscorlib"

List<String>

<scg:List x:TypeArguments="sys:String" ...> instantiates a new List<T> with a String type argument.

Dictionary<String,String>

<scg:Dictionary x:TypeArguments="sys:String,sys:String" ...> instantiates a new Dictionary<TKey,TValue> with two String type arguments.

Queue<KeyValuePair<String,String>>

<scg:Queue x:TypeArguments="scg:KeyValuePair(sys:String,sys:String)" ...> instantiates a new Queue<T> that has a constraint of KeyValuePair<TKey,TValue> with the inner constraint type arguments String and String.

XAML 2006 and WPF Generic XAML Usages

For XAML 2006 usage, and XAML that is used for WPF applications, the following restrictions exist for x:TypeArguments and generic type usages from XAML in general:

  • Only the root element of a XAML file can support a generic XAML usage that references a generic type.

  • The root element must map to a generic type with at least one type argument. An example is PageFunction<T>. The page functions are the primary scenario for XAML generic usage support in WPF.

  • The root element XAML object element for the generic must also declare a partial class using x:Class. This is true even if defining a WPF build action.

  • x:TypeArguments cannot reference nested generic constraints.

XAML 2009 or XAML 2006 with No WPF 3.0 or WPF 3.5 Dependency

In .NET XAML Services for either XAML 2006 or XAML 2009, the WPF-related restrictions on generic XAML usage are relaxed. You can instantiate a generic object element at any position in XAML markup that the backing type system and object model can support.

If you use XAML 2009 instead of mapping the CLR base types to obtain XAML types for common language primitives, you can use Built-in Types for Common XAML Language Primitives as information items in a typeString. For example, you could declare the following (prefix mappings not shown, but x is the XAML language XAML namespace for XAML 2009):

<my:BusinessObject x:TypeArguments="x:String,x:Int32"/>

In WPF and when targeting .NET Framework 4 or .NET Core 3.0 (or later), you can use XAML 2009 features together with x:TypeArguments but only for loose XAML (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. If you need to markup compile the XAML, you must operate under the restrictions noted in the XAML 2006 and WPF Generic XAML Usages section. BAML is only supported in .NET Framework.

See also