简体   繁体   English

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

[英]“LoadTemplate should not be null” with property ItemTemplate in CollectionView with Xamarin Forms

I'm stuck in this problem in first try to use xamarin forms. 我在第一次尝试使用xamarin表格时遇到了这个问题。

I'm trying to set DataTemplateSelector for CollectionView in Xamarin forms but I get this error "LoadTemplate should not be null". 我正在尝试在Xamarin表单中为CollectionView设置DataTemplateSelector,但是我收到此错误“LoadTemplate不应为null”。

It working without problems if I used ListView. 如果我使用ListView,它可以正常工作。

this is my XAML Code : 这是我的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> 

I can't figure out the problem. 我无法弄清楚这个问题。 Can anyone help me please ? 有人可以帮我吗?

you define the DataTemplateSelector in your <ResourceDictionary> 您在<ResourceDictionary>定义DataTemplateSelector

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

you could use it in your CollectionView like this: 您可以在CollectionView中使用它,如下所示:

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

more info refer to link: DataTemplateSelector 更多信息请参考链接: DataTemplateSelector

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM