"LoadTemplate should not be null" 在具有 Xamarin 窗体的集合视图中使用属性项模板



我在第一次尝试使用 xamarin 表单时遇到了这个问题。

我正在尝试在Xamarin表单中为CollectionView设置DataTemplateSelector,但是我收到此错误"LoadTemplate不应该为空"。

如果我使用 ListView,它可以正常工作。

这是我的 XAML 代码:

<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
                 xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
                 xmlns:viewmodel="clr-namespace:JisrWallet.ViewModels"
                 xmlns:common="clr-namespace:JisrWallet.Common"
                 x:Class="JisrWallet.views.Layout.CardsPage"
                 xmlns:views="clr-namespace:SuaveControls.Views;assembly=SuaveControls.FloatingActionButton"
                 BackgroundColor="{StaticResource lightGray}">
    
        <ContentPage.Resources>
            <ResourceDictionary>
                <common:IntColorToHexConverter x:Key="colorConverter"/>
                <common:PrefixValueConverter x:Key="prefixConverter"/>
                <DataTemplate x:Key="existCardTemplate">
                    <StackLayout Margin="2">
                        <Image Source="{Binding StoreImageUrl}" 
                                   Aspect="AspectFill"
                                   HeightRequest="120"
                                   BackgroundColor="{Binding CardColor, Converter={StaticResource colorConverter}}"/>
                    </StackLayout>
                </DataTemplate>
    
                <DataTemplate x:Key="newCardTemplate">
                       
                        <StackLayout BackgroundColor="{Binding CardColor, Converter={StaticResource colorConverter}}">
                            <Label Text="{Binding CardName, Converter={StaticResource prefixConverter}, ConverterParameter=1}"
                                       HeightRequest="120"
                                       HorizontalOptions="Center"
                                       VerticalOptions="Center"
                                       VerticalTextAlignment="Center"
                                       TextColor="White"/>
                            <Label Text="{Binding CardName}" 
                                           FontAttributes="Bold"
                                           BackgroundColor="{Binding CardColor, Converter={StaticResource colorConverter}}"
                                           TextColor="{StaticResource whiteColor}"
                                           HorizontalTextAlignment="Center"/>
                            <BoxView HeightRequest="2" BackgroundColor="White"/>
                        </StackLayout>
                </DataTemplate>
    
                <common:CardItemTemplateSelector x:Key="cardItemTemplateSelector"
                                                 ExistCardTemplate="{StaticResource existCardTemplate}"
                                                 NewCardTemplate="{StaticResource newCardTemplate}"/>
            </ResourceDictionary>
        </ContentPage.Resources>
    
        <StackLayout>
            <StackLayout.BindingContext>
                <viewmodel:CardsViewModel/>
            </StackLayout.BindingContext>
    
            <CollectionView x:Name="collectionView" 
                            ItemsSource="{Binding Cards}"
                            ItemTemplate="{StaticResource newCardTemplate}"
                            SelectionMode="Single"
                            SelectionChanged="CollectionView_SelectionChanged">
                <CollectionView.ItemsLayout >
                    <GridItemsLayout Orientation="Vertical" Span="2" />
                </CollectionView.ItemsLayout>
            </CollectionView>
    
    
            <views:FloatingActionButton Image="ic_add_white"
                                        ButtonColor="{StaticResource AccentColor}"
                                        WidthRequest="56"
                                        HeightRequest="56"
                                        HorizontalOptions="End"
                                        VerticalOptions="CenterAndExpand"
                                        Margin="8"
                                        Clicked="FloatingActionButton_Clicked"/>
        </StackLayout>
    
    </ContentPage>

我无法弄清楚问题所在。谁能帮我?

您可以在<ResourceDictionary>中定义 DataTemplateSelector

<ResourceDictionary>
  ...
  <common:CardItemTemplateSelector x:Key="cardItemTemplateSelector"
                                             ExistCardTemplate="{StaticResource existCardTemplate}"
                                             NewCardTemplate="{StaticResource newCardTemplate}"/>
</ResourceDictionary>

您可以在 CollectionView 中像这样使用它:

<CollectionView>
      ...
     ItemTemplate="{StaticResource cardItemTemplateSelector}"         
</CollectionView>

更多信息请参阅链接:数据模板选择器

最新更新