Forum Xamarin Xamarin.Forms

CollectionView Data isn't loaded

AkamomAkamom Member ✭✭
edited March 10 in Xamarin.Forms

Hi everyone,

I hope you can help me with my issue.

I created a ContentPage with the following ListView. So the important part is the TemplateForAndroid.

<ContentPage.Resources>
        <ResourceDictionary>
            <DataTemplate x:Key="TemplateForiOS">
                <views:NotificationViewCelliOS/>
            </DataTemplate>
            <DataTemplate x:Key="TemplateForAndroid">
                <views:NotificationViewCell/>
            </DataTemplate>
        </ResourceDictionary>
    </ContentPage.Resources>
<ListView 
    x:Name="InboxListView"
    x:FieldModifier="public"
    ItemsSource="{Binding Notifications}"
    HasUnevenRows="True" 
    VerticalOptions="StartAndExpand"
    IsPullToRefreshEnabled="True"
    VerticalScrollBarVisibility="Never"
    SelectionMode="None"
    ItemAppearing="InboxListView_OnItemAppearing"
    RefreshCommand="{Binding RefreshCommand}"
    IsRefreshing="{Binding IsRefreshing}"
    Scrolled="InboxListView_OnScrolled">

    <ListView.Behaviors>
        <local:EventToCommandBehaviour EventName="ItemAppearing" Command="{Binding LoadMoreCommand}"/>
    </ListView.Behaviors>

    <ListView.ItemTemplate>
            <OnPlatform x:TypeArguments="DataTemplate">
                <On Platform="iOS" Value="{StaticResource TemplateForiOS}"/>
                <On Platform="Android" Value="{StaticResource TemplateForAndroid}"/>
            </OnPlatform>
    </ListView.ItemTemplate>
</ListView>

The TemplateForAndroid looks as following:

<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             xmlns:converter="clr-namespace:CommunityApp.Converter;assembly=CommunityApp"
             x:Class="CommunityApp.Views.NotificationViewCell">
    <ViewCell.View>
        <CollectionView x:Name="NotiScroll"
                        ItemsLayout="HorizontalList">
            <CollectionView.Resources>
                <ResourceDictionary>
                    <converter:HtmlLabelConverter x:Key="HtmlLabelConverter" />
                </ResourceDictionary>
            </CollectionView.Resources>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Orientation="Horizontal">
                        <Label Grid.Row="0" Grid.Column="0"
                               HeightRequest="30"
                               Text="Test"
                               FontAttributes="Italic"
                               VerticalOptions="End" />
                        <Label Grid.Row="0" Grid.Column="0"
                               HeightRequest="30"
                               Text="Test"
                               FontAttributes="Italic"
                               VerticalOptions="End" />
                        <Label Grid.Row="0" Grid.Column="0"
                               HeightRequest="30"
                               Text="Test"
                               FontAttributes="Italic"
                               VerticalOptions="End" />
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </ViewCell.View>
</ViewCell>

At the moment I try to work with dummy Labels, but even those are not visible in my Android App. So The ListView works good but the Labels from the DataTemplate inside the ViewCell isn't visible or doesn't get loaded.

If you have further questions, please ask me.

Greetings

Best Answer

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    You should set ItemsSource on the CollectionView in custom viewcell .

     <CollectionView ItemsLayout="HorizontalList" ItemsSource="{Binding .}">
    
  • ColeXColeX Member, Xamarin Team Xamurai

    Does my answer help or not ?

  • AkamomAkamom Member ✭✭

    Thank you for your fast response. :)

    Sadly this didn't work for me. I also tried to copy my collection view inside my ContentPage to see if this does the trick (maybe the error appears because of the custom ViewCell), but still, the DataTemplate isn't visible.

  • AkamomAkamom Member ✭✭

    Thank you for this sample. Figgered out, that it doesn't work what I tried to achieve, because my ItemSource is not from type IEnumerable (at least I think, that this is the problem).

    I tried to work with CollectionView to use the SnapPoint feature.

    So the last question. Is there a way to use SnapPoint or something comparable for ScrollViews?
    (see Microsoft Doc -> CollectionView -> Scrolling -> Snappoint)

Sign In or Register to comment.