Nicole
Nicole

Reputation: 23

CollectionView - Binding Data doesn't work

I'm exercising on my first MAUI application, however, I have problem to view the data. With debugging I can see the list is not empty, there is data, as well I'm passing the list data to ItemSource. What could be the issue?

Screenshot

My code on onAppearing():

    protected override async void OnAppearing()
    {
        base.OnAppearing();
        ApiService service = new ApiService();
        var newsResult = await service.GetNews();

        foreach(var item in newsResult.Articles)
        {
            ArticleList.Add(item);
        }

        CvNews.ItemsSource= ArticleList;

    }

xaml:

        <CollectionView Grid.Row="2" x:Name="CvNews">
            <CollectionView.ItemsLayout>
                <LinearItemsLayout Orientation="Vertical" ItemSpacing="15"/>
            </CollectionView.ItemsLayout>
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="10">
                        <Image HeightRequest="200" Source="{Binding Image}"/>
                        <Label FontSize="Medium" Text="{Binding Title}"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>

In the image source I add default image from the resources, and the image is displayed

Article:

public class Article
    {
        [JsonProperty("title")]
        public string Title;

        [JsonProperty("description")]
        public string Description;

        [JsonProperty("content")]
        public string Content;

        [JsonProperty("url")]
        public string Url;

        [JsonProperty("image")]
        public string Image;

        [JsonProperty("publishedAt")]
        public DateTime PublishedAt;

        [JsonProperty("source")]
        public Source Source;
    }

    public class Root
    {
        [JsonProperty("totalArticles")]
        public int TotalArticles;

        [JsonProperty("articles")]
        public List<Article> Articles;
    }

    public class Source
    {
        [JsonProperty("name")]
        public string Name;

        [JsonProperty("url")]
        public string Url;
    }

Upvotes: 2

Views: 1216

Answers (1)

Jason
Jason

Reputation: 89082

you can only bind to public properties

<Image HeightRequest="200" Source="{Binding Image}"/>
<Label FontSize="Medium" Text="{Binding Title}"/>

neither Image nor Title are public properties

public string Title;
    

to make it a property

public string Title { get; set; }

Upvotes: 2

Related Questions