泛型
.NET 多平臺應用程式 UI (.NET MAUI) XAML 藉由將泛型條件約束指定為類型自變數,提供取用泛型 CLR 類型的支援。 這個支援是由 x:TypeArguments
指示詞所提供,它將泛型的限制型別參數傳遞給泛型型別的建構函式。
類型自變數會指定為字串,而且通常會加上前置詞,例如 sys:String
和 sys:Int32
。 需要使用前置字詞,因為 CLR 的泛型約束類型通常來自未映射到預設 .NET MAUI 命名空間的程式庫。 不過,XAML 2009 內建類型,例如 x:String
和 x:Int32
,也可以指定為類型自變數,其中 x
是 XAML 2009 的 XAML 語言命名空間。 如需 XAML 2009 內建類型的詳細資訊,請參閱 XAML 2009 語言基本類型。
重要
不支援在 .NET MAUI XAML 中使用 x:TypeArguments
指示詞定義泛型型別。
您可以使用逗號分隔符來指定多個類型自變數。 此外,如果泛型條件約束使用泛型類型,巢狀條件約束類型自變數應該包含在括弧中。
注意
x:Type
標記延伸提供泛型型別的 Common Language Runtime (CLR) 型別參考,且具有與 C# 中的 typeof
運算符類似的函式。 如需詳細資訊,請參閱 x:Type 標記延伸。
如需使用 x:DataType
和 x:Type
指示詞在 .NET MAUI XAML 中指定泛型型別的相關資訊,請參閱 編譯系結設定泛型型別 和 x:Type 標記延伸。
單一基本類型自變數
您可以使用 x:TypeArguments
指示詞,將單一基本類型自變數指定為前置字串自變數:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=netstandard"
...>
<CollectionView>
<CollectionView.ItemsSource>
<scg:List x:TypeArguments="x:String">
<x:String>Baboon</x:String>
<x:String>Capuchin Monkey</x:String>
<x:String>Blue Monkey</x:String>
<x:String>Squirrel Monkey</x:String>
<x:String>Golden Lion Tamarin</x:String>
<x:String>Howler Monkey</x:String>
<x:String>Japanese Macaque</x:String>
</scg:List>
</CollectionView.ItemsSource>
</CollectionView>
</ContentPage>
在此範例中,System.Collections.Generic
定義為 scg
XAML 命名空間。
CollectionView.ItemsSource
屬性設定為一個使用 XAML 2009 內建 x:String
類型以類型參數具現化的 string
型別的 List<T>
。
List<string>
集合會使用多個 string
專案初始化。
或者,但同樣地,List<T>
集合可以使用CLR String
類型具現化:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=netstandard"
xmlns:sys="clr-namespace:System;assembly=netstandard"
...>
<CollectionView>
<CollectionView.ItemsSource>
<scg:List x:TypeArguments="sys:String">
<sys:String>Baboon</sys:String>
<sys:String>Capuchin Monkey</sys:String>
<sys:String>Blue Monkey</sys:String>
<sys:String>Squirrel Monkey</sys:String>
<sys:String>Golden Lion Tamarin</sys:String>
<sys:String>Howler Monkey</sys:String>
<sys:String>Japanese Macaque</sys:String>
</scg:List>
</CollectionView.ItemsSource>
</CollectionView>
</ContentPage>
單一物件類型自變數
您可以使用 x:TypeArguments
指示詞,將單一物件類型自變數指定為前置字串自變數:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:GenericsDemo.Models"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=netstandard"
...>
<CollectionView>
<CollectionView.ItemsSource>
<scg:List x:TypeArguments="models:Monkey">
<models:Monkey Name="Baboon"
Location="Africa and Asia"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg" />
<models:Monkey Name="Capuchin Monkey"
Location="Central and South America"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg" />
<models:Monkey Name="Blue Monkey"
Location="Central and East Africa"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/BlueMonkey.jpg/220px-BlueMonkey.jpg" />
</scg:List>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="models:Monkey">
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Name}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage>
在此範例中,GenericsDemo.Models
定義為 models
XAML 命名空間,System.Collections.Generic
定義為 scg
XAML 命名空間。
CollectionView.ItemsSource
屬性被設定為用 Monkey
類型參數具體化的 List<T>
。
List<Monkey>
集合會使用多個 Monkey
項目初始化,而定義每個 Monkey
物件外觀的 DataTemplate 會被設定為 CollectionView的 ItemTemplate
。
多個類型自變數
您可以使用 x:TypeArguments
指示詞,將多個型別自變數指定為前置字元串自變數,並以逗號分隔。 當泛型條件約束使用泛型類型時,巢狀條件約束類型自變數會包含在括弧中:
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:models="clr-namespace:GenericsDemo.Models"
xmlns:scg="clr-namespace:System.Collections.Generic;assembly=netstandard"
...>
<CollectionView>
<CollectionView.ItemsSource>
<scg:List x:TypeArguments="scg:KeyValuePair(x:String,models:Monkey)">
<scg:KeyValuePair x:TypeArguments="x:String,models:Monkey">
<x:Arguments>
<x:String>Baboon</x:String>
<models:Monkey Location="Africa and Asia"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Papio_anubis_%28Serengeti%2C_2009%29.jpg/200px-Papio_anubis_%28Serengeti%2C_2009%29.jpg" />
</x:Arguments>
</scg:KeyValuePair>
<scg:KeyValuePair x:TypeArguments="x:String,models:Monkey">
<x:Arguments>
<x:String>Capuchin Monkey</x:String>
<models:Monkey Location="Central and South America"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/4/40/Capuchin_Costa_Rica.jpg/200px-Capuchin_Costa_Rica.jpg" />
</x:Arguments>
</scg:KeyValuePair>
<scg:KeyValuePair x:TypeArguments="x:String,models:Monkey">
<x:Arguments>
<x:String>Blue Monkey</x:String>
<models:Monkey Location="Central and East Africa"
ImageUrl="https://upload.wikimedia.org/wikipedia/commons/thumb/8/83/BlueMonkey.jpg/220px-BlueMonkey.jpg" />
</x:Arguments>
</scg:KeyValuePair>
</scg:List>
</CollectionView.ItemsSource>
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="scg:KeyValuePair(x:String,models:Monkey)">
<Grid Padding="10">
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="Auto" />
</Grid.ColumnDefinitions>
<Image Grid.RowSpan="2"
Source="{Binding Value.ImageUrl}"
Aspect="AspectFill"
HeightRequest="60"
WidthRequest="60" />
<Label Grid.Column="1"
Text="{Binding Key}"
FontAttributes="Bold" />
<Label Grid.Row="1"
Grid.Column="1"
Text="{Binding Value.Location}"
FontAttributes="Italic"
VerticalOptions="End" />
</Grid>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
</ContentPage
在此範例中,GenericsDemo.Models
定義為 models
XAML 命名空間,System.Collections.Generic
定義為 scg
XAML 命名空間。
CollectionView.ItemsSource
屬性會設定為使用 KeyValuePair<TKey, TValue>
條件約束具現化的 List<T>
,而內部條件約束類型自變數 string
和 Monkey
。 使用多個 KeyValuePair
項目,通過非預設的 KeyValuePair
建構函式初始化 List<KeyValuePair<string,Monkey>>
集合,並且定義每個 Monkey
對象外觀的 DataTemplate 被設定為 CollectionView的 ItemTemplate
。 如需將自變數傳遞至非預設建構函式的資訊,請參閱 傳遞建構函式自變數。