共用方式為


XAML 中的 Xamarin.Forms 泛型

Xamarin.Forms XAML 藉由將泛型條件約束指定為類型自變數,提供取用泛型 CLR 類型的支援。 這個支援是由 x:TypeArguments 指示詞所提供,該指示詞會將泛型的限制型別自變數傳遞至泛型型別的建構函式。

重要

不支援在 XAML x:TypeArguments 中使用 Xamarin.Forms 指示詞定義泛型類別。

類型自變數會指定為字串,而且通常會加上前置詞,例如 sys:Stringsys:Int32。 需要前置詞,因為 CLR 泛型條件約束的一般類型來自未對應至預設命名空間的連結 Xamarin.Forms 庫。 不過,XAML 2009 內建類型,例如 x:Stringx:Int32,也可以指定為類型自變數,其中 x 是 XAML 2009 的 XAML 語言命名空間。 如需 XAML 2009 內建類型的詳細資訊,請參閱 XAML 2009 語言基本類型

您可以使用逗號分隔符來指定多個類型自變數。 此外,如果泛型條件約束使用泛型類型,巢狀條件約束類型自變數應該包含在括弧中。

注意

標記 x:Type 延伸會提供泛型型別的 CLR 型別參考,而且具有與 C# 中的 運算符類似的函 typeof 式。 如需詳細資訊,請參閱 x:Type 標記延伸

單一基本類型自變數

您可以使用 指示詞,將單一基本類型自變數指定為前置字串自變數 x:TypeArguments

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             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會設定為List<T>使用 XAML 2009 內x:String建型別具現化string類型自變數的 。 集合 List<string> 會使用多個 string 專案初始化。

或者,但同樣地, List<T> 可以使用CLR String 類型具現化集合:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             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://xamarin.com/schemas/2014/forms"
             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.Models 定義為 models XAML 命名空間,並 System.Collections.Generic 定義為 scg XAML 命名空間。 屬性 CollectionView.ItemsSource 會設定為 List<T> 使用 Monkey 型別自變數具現化的 。 集合 List<Monkey> 會使用多個 Monkey 項目初始化,而 DataTemplate 定義每個 Monkey 物件外觀的 會設定為 ItemTemplateCollectionView

多個類型自變數

您可以使用 指示詞,將多個類型自變數指定為前置字串自變數, x:TypeArguments 並以逗號分隔。 當泛型條件約束使用泛型類型時,巢狀條件約束類型自變數會包含在括弧中:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
             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.Models 定義為 models XAML 命名空間,並 System.Collections.Generic 定義為 scg XAML 命名空間。 屬性 CollectionView.ItemsSource 會設定為 List<T> 使用 KeyValuePair<TKey, TValue> 條件約束具現化的 ,其中包含內部條件約束類型自變數 stringMonkey。 集合List<KeyValuePair<string,Monkey>>會使用非預設KeyValuePair建構函式,使用多個KeyValuePair專案初始化,而 DataTemplate 定義每個Monkey物件外觀的 會設定為 ItemTemplateCollectionView。 如需將自變數傳遞至非預設建構函式的資訊,請參閱 傳遞建構函式自變數。