Hi guys, im using Xamarin.Forms and im having a problem trying to use alexrainman's Carousel View.
Particularly, i want to set up a Carousel View in which each view is a ListView , and then each ListView would display a List of objects.
I have read through the document pages and define the sources for my CarouselView and ListView as follow:
ObservableCollection<ObservableCollection<ItemFormat>> MyCarouselSource = new ObservableCollection<ObservableCollection<ItemFormat>>();
ObservableCollection<ItemFormat> MyListViewSource = new ObservableCollection<ItemFormat>();
but i can not get it to work.
My ListView would have a list of objects, something like this:
[
{
"field_name":"value1",
"field_value":"value11"
},
{
"field_name":"value2",
"field_value":"value22"
},
...
]
Here is my code:
public class ItemFormat
{
[JsonProperty("field_name")]
public string FieldName { get; set; }
[JsonProperty("field_value")]
public string FieldValue { get; set; }
}
View Model (ValidationBindableBase has the INotifyPropertyChanged in it):
public class MyViewModel : ValidationBindableBase
{
...
ObservableCollection<ObservableCollection<ItemFormat>> _myCarouselSource;
public ObservableCollection<ObservableCollection<ItemFormat>> MyCarouselSource
{
get => _myCarouselSource;
set => SetProperty(ref _myCarouselSource, value);
}
ObservableCollection<ItemFormat> _myListViewSource;
public ObservableCollection<ItemFormat> MyListViewSource
{
get => _myListViewSource;
set => SetProperty(ref _myListViewSource, value);
}
public DetailedSalaryViewModel()
{
}
}
code behind
public partial class MyViewPage : ContentPage
{
MyViewModel viewModel = new MyViewModel();
public MyViewPage ()
{
InitializeComponent ();
BindingContext = viewModel ;
}
}
The XAML view:
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="salary_sheet.Views.MyViewPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:controls="clr-namespace:CarouselView.FormsPlugin.Abstractions;assembly=CarouselView.FormsPlugin.Abstractions">
<controls:CarouselViewControl
x:Name="carousel"
ItemsSource="{Binding MyCarouselSource}"
Orientation="Horizontal"
ShowArrows="true"
ShowIndicators="true">
<controls:CarouselViewControl.ItemTemplate>
<DataTemplate>
<ListView ItemsSource="{Binding MyListViewSource}" RowHeight="100">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Padding="5">
<Grid.ColumnDefinitions>
<ColumnDefinition Width=".5*" />
<ColumnDefinition Width=".5*" />
</Grid.ColumnDefinitions>
<Label
Grid.Column="0"
HorizontalOptions="Start"
Text="{Binding FieldName}" />
<Label
Grid.Column="1"
HorizontalOptions="Start"
Text="{Binding FieldValue}" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</DataTemplate>
</controls:CarouselViewControl.ItemTemplate>
</controls:CarouselViewControl>
</ContentPage>
Please help me to correct the definition of the sources for my ListView and CarouselView.
Also how to establish and organize the view's XAML code.
I really appreciate all your help. Thank you.