Skip to content

Commit

Permalink
Generic types for XAML compiled bindings (#2748)
Browse files Browse the repository at this point in the history
* Generic type support for x:DataType and x:Type.

* Fix bookmark.

* Edits.
  • Loading branch information
davidbritch authored Jan 27, 2025
1 parent b46d4e8 commit 5c39a62
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 4 deletions.
26 changes: 25 additions & 1 deletion docs/fundamentals/data-binding/compiled-bindings.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Compiled bindings"
description: "Compiled bindings can be used to improve data binding performance in .NET MAUI applications."
ms.date: 11/14/2024
ms.date: 01/27/2025
---

# Compiled bindings
Expand Down Expand Up @@ -136,6 +136,30 @@ The following example demonstrates correctly setting the `x:DataType` on a <xref

While this example sets the `x:DataType` attribute to a string literal, it can also be set to a type with the `x:Type` markup extension. For more information about the `x:Type` markup extension, see [x:Type Markup Extension](~/xaml/markup-extensions/consume.md#xtype-markup-extension).

### Compile bindings that specify a generic type

Generic types can be specified with the `x:DataType` attribute by specifying the generic constraint as a prefixed string argument in parentheses:

```xaml
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyMauiApp"
x:Class="MyMauiApp.MyPage"
x:DataType="local:MyViewModel(x:Boolean)">
...
</ContentPage>
```

Multiple type arguments can be specified as prefixed string arguments, delimited by a comma:

```xaml
<DataTemplate x:DataType="local:MyType(local:MyObject,x:Boolean)">
...
</DataTemplate>
```

For more information about generics in XAML, see [Generics](~/xaml/generics.md).

::: moniker range=">=net-maui-9.0"

### Compile bindings that define the `Source` property
Expand Down
6 changes: 4 additions & 2 deletions docs/xaml/generics.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Generics"
description: ".NET MAUI XAML provides support for consuming generic CLR types by specifying the generic constraints as type arguments."
ms.date: 01/24/2022
ms.date: 01/27/2025
---

# Generics
Expand All @@ -11,13 +11,15 @@ ms.date: 01/24/2022
Type arguments are specified as a string, and are typically prefixed, such as `sys:String` and `sys:Int32`. Prefixing is required because the typical types of CLR generic constraints come from libraries that are not mapped to the default .NET MAUI namespaces. However, the XAML 2009 built-in types such as `x:String` and `x:Int32`, can also be specified as type arguments, where `x` is the XAML language namespace for XAML 2009. For more information about the XAML 2009 built-in types, see [XAML 2009 Language Primitives](/dotnet/desktop-wpf/xaml-services/types-for-primitives#xaml-2009-language-primitives).

> [!IMPORTANT]
> Defining generic classes in .NET MAUI XAML, with the `x:TypeArguments` directive, is unsupported.
> Defining generic types in .NET MAUI XAML, with the `x:TypeArguments` directive, is unsupported.
Multiple type arguments can be specified by using a comma delimiter. In addition, if a generic constraint uses generic types, the nested constraint type arguments should be contained in parentheses.

> [!NOTE]
> The `x:Type` markup extension supplies a Common Language Runtime (CLR) type reference for a generic type, and has a similar function to the `typeof` operator in C#. For more information, see [x:Type markup extension](~/xaml/markup-extensions/consume.md#xtype-markup-extension).
For information about specifying generic types in .NET MAUI XAML, with the `x:DataType` and `x:Type` directives, see [Compile bindings that specify a generic type](~/fundamentals/data-binding/compiled-bindings.md#compile-bindings-that-specify-a-generic-type) and [x:Type markup extension](~/xaml/markup-extensions/consume.md#xtype-markup-extension).

## Single primitive type argument

A single primitive type argument can be specified as a prefixed string argument using the `x:TypeArguments` directive:
Expand Down
20 changes: 19 additions & 1 deletion docs/xaml/markup-extensions/consume.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
---
title: "Consume XAML markup extensions"
description: ".NET MAUI XAML markup extensions enhance the power and flexibility of XAML by allowing element attributes to be set from a variety of sources."
ms.date: 04/18/2023
ms.date: 01/27/2025
---

# Consume XAML markup extensions
Expand Down Expand Up @@ -221,6 +221,24 @@ When a <xref:Microsoft.Maui.Controls.Button> is pressed a new instance of the `C

:::image type="content" source="media/consume/typedemo.png" alt-text="x:Type demo.":::

Generic types can be specified with the `x:Type` markup extension by specifying the generic constraint as a prefixed string argument in parentheses:

```xaml
<x:Array Type="{x:Type local:MyType(local:MyObject)}">
...
</x:Array>
```

Multiple type arguments can be specified as prefixed string arguments, delimited by a comma:

```xaml
<x:Array Type="{x:Type local:MyType(local:MyObject,x:Boolean)}">
...
</x:Array>
```

For more information about generics in XAML, see [Generics](~/xaml/generics.md).

## x:Array markup extension

The `x:Array` markup extension enables you to define an array in markup. It is supported by the <xref:Microsoft.Maui.Controls.Xaml.ArrayExtension> class, which defines two properties:
Expand Down

0 comments on commit 5c39a62

Please sign in to comment.