I created a new class to wrap the title and image in a way that suits my needs, as shown below:
public class CarouselItemHomePage
{
public string Title { get; set; }
public string Image { get; set; }
}
Next, I declared an ObservableCollection
of this class and bound it to a CarouselView
in XAML like this:
<CarouselView ItemsSource="{Binding CarouselItems}"
HeightRequest="200"
IndicatorView="CrIndicator"
Loop="True">
<CarouselView.ItemTemplate>
<DataTemplate x:DataType="model:CarouselItemHomePage">
<Image Source="{Binding Image}"
Aspect="AspectFill"
HeightRequest="200"/>
</DataTemplate>
</CarouselView.ItemTemplate>
</CarouselView>
In the OnAppearing
method, I populate the collection as follows:
CarouselItems.Clear();
CarouselItems.Add(new CarouselItemHomePage
{
Title = "cr1.png",
Image = "cr1.png"
});
CarouselItems.Add(new CarouselItemHomePage
{
Title = "cr2.png",
Image = "cr2.png"
});
CarouselItems.Add(new CarouselItemHomePage
{
Title = "cr3.png",
Image = "cr3.png"
});
CarouselItems.Add(new CarouselItemHomePage
{
Title = "cr4.png",
Image = "cr4.png"
});
This setup works well for me. Thank you very much to everyone who has helped me with this. I truly appreciate your support and guidance.