使用Xamarin Forms在CollectionView中使用属性ItemTemplate“LoadTemplate不应为null”

问题描述 投票:0回答:1

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

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

如果我使用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>

我无法弄清楚这个问题。有人可以帮我吗?

c# xaml xamarin xamarin.forms xamarin.ios
1个回答
© www.soinside.com 2019 - 2024. All rights reserved.