ジェネリック

.NET マルチプラットフォーム アプリ UI (.NET MAUI) XAML では、ジェネリック制約を型引数として指定することで、ジェネリック CLR 型の使用に対応しています。 この対応は、ジェネリックの制約型引数をジェネリック型のコンストラクターに渡す x:TypeArguments ディレクティブによって提供されます。

型引数は文字列として指定され、通常は、sys:Stringsys:Int32 のようなプレフィックスが付けられます。 CLR ジェネリック制約の一般的な型は、既定の .NET MAUI 名前空間にマッピングされていないライブラリから取得されるため、プレフィックスが必要です。 ただし、XAML 2009 組み込み型 (x:Stringx:Int32 など) は型引数として指定することもできます。この場合は、x が XAML 2009 の XAML 言語名前空間です。 XAML 2009 組み込み型の詳細については、「XAML 2009 言語プリミティブ」を参照してください。

重要

x:TypeArguments ディレクティブを使用した .NET MAUI XAML でのジェネリック クラスの定義はサポートされていません。

コンマ区切り記号を使用して、複数の型引数を指定できます。 さらに、ジェネリック制約でジェネリック型を使用する場合は、入れ子になった制約型の引数をかっこで囲む必要があります。

x:Type マークアップ拡張機能は、ジェネリック型の共通言語ランタイム (CLR) 型参照を提供し、C# の typeof 演算子と同様の関数を持ちます。 詳細については、「 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.Genericscg XAML 名前空間として定義されています。 CollectionView.ItemsSource プロパティは、XAML 2009 組み込み x:String 型を使用して string 型引数でインスタンス化された List<T> に設定されます。 List<string> コレクションは複数の string 項目で初期化されます。

また、CLR String 型を使用して List<T> コレクションをインスタンス化することもできます。

<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>
                <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.Modelsmodels XAML 名前空間として定義され、System.Collections.Genericscg XAML 名前空間として定義されています。 CollectionView.ItemsSource プロパティは、Monkey 型引数でインスタンス化された List<T> に設定されます。 List<Monkey> コレクションは、複数の Monkey 項目で初期化され、各 Monkey オブジェクトの外観を定義する DataTemplateCollectionViewItemTemplate として定義されます。

複数の型引数

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>
                <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.Modelsmodels XAML 名前空間として定義され、System.Collections.Genericscg XAML 名前空間として定義されています。 CollectionView.ItemsSource プロパティは、内部制約型引数 stringMonkeyKeyValuePair<TKey, TValue> 制約を使用してインスタンス化される List<T> に設定されます。 List<KeyValuePair<string,Monkey>> コレクションは、既定以外の KeyValuePair コンストラクターを使用して、複数の KeyValuePair 項目で初期化されます。各 Monkey オブジェクトの外観を定義する DataTemplateCollectionViewItemTemplate として設定されます。 既定以外のコンストラクターに引数を渡す方法については、「コンストラクター引数を渡す」を参照してください。