0
votes

I am using xamarin carousel view. Salon is my parent list and SalonService is called as features in each Salon list item.

My code displays only one item per view. I want to display three (3) items on each carousel view.

Below is my code for lists:

Ftrs.Add(new SalonService() { Services = "Test 1", Prices = "$150" });
Ftrs.Add(new SalonService() { Services = "Test 2", Prices = "$150" });
Ftrs.Add(new SalonService() { Services = "Test 3", Prices = "$150" });
Ftrs.Add(new SalonService() { Services = "Test 4", Prices = "$150" });

salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 1", Features = Ftrs });
salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 2", Features = Ftrs });
salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 3", Features = Ftrs });
salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 4", Features = Ftrs });
salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 5", Features = Ftrs });
salons.Add(new Salon() { ImgUrl = "test.png", Title = "Test Title 6", Features = Ftrs });

Below is my code for carousel view:

foreach (var item in data)
{
 DataTemplate salonDataTemplate = new DataTemplate(() =>
 {
    // some code
 });

Resources = new ResourceDictionary();
Resources.Add("salonTemplate", salonDataTemplate);

int countIndex = 1;
int newPageCount = 1;

foreach (var newItem in item.Features)
{
    if (newPageCount <= 3)
    {
        //same carousal view
    }
    else
    {
        countIndex++;
        //new carousal view
    }
    newPageCount++;
} }

var list_Featured = new CarouselView()
{
    BackgroundColor = Color.White,
    //ItemsSource = item.Features,
    ItemsSource = newList,
        ItemTemplate = (DataTemplate)Resources["salonTemplate"], HeightRequest = 150 
};

StackLayout stkList = new StackLayout();
stkList.IsVisible = false;
stkList.Children.Add(list_Featured);

Current Output:

  • Test Title 1
  • Test 1

Expected Output:

  • Test Title 1
  • Test 1
  • Test 2
  • Test 3

My carousel view is only displaying one item per view. I want to display three items per view.

1
Sorry, but I'm missing what your exact problem is. Where does it go wrong? What did you expect? Do you have any clues? Please refer to the How to ask page to read up on how to ask a good question on SO.Gerald Versluis
My carousel view is only displaying one item per view. I want to display three items per view.Zain SMJ
Can you please show your xaml code? What is the template for carouselview's item?Grace Feng

1 Answers

0
votes

I achieved my desired result by creating a list of lists.

Something like this:

List<List<string>> myList = new List<List<string>>();
myList.Add(new List<string> { "a", "b" });
myList.Add(new List<string> { "c", "d", "e" });