2

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;
    }
4
  • please post the code for Article Commented Nov 20, 2022 at 15:47
  • you can only bind to public properties. None of the items in Article are C# properties Commented Nov 20, 2022 at 16:10
  • Move the code in the OnAppearing() method to the Code Behind and then check. Commented Nov 20, 2022 at 16:32
  • You can also refer the official doc: Populate a CollectionView with data. Commented Mar 1, 2023 at 9:03

1 Answer 1

2

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; }
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.