【发布时间】:2019-07-07 12:08:48
【问题描述】:
我正在使用 Xamarin.Forms,但在尝试使用 alexrainman 的 Carousel View 时遇到问题。
特别是,我想设置一个轮播视图,其中每个视图都是一个 ListView ,然后每个 ListView 将显示一个对象列表。
我已通读文档页面并为我的CarouselView 和ListView 定义如下来源:
ObservableCollection<ObservableCollection<ItemFormat>> MyCarouselSource = new ObservableCollection<ObservableCollection<ItemFormat>>();
ObservableCollection<ItemFormat> MyListViewSource = new ObservableCollection<ItemFormat>();
但我无法让它工作。
我的 ListView 会有一个对象列表,如下所示:
[
{
"field_name":"value1",
"field_value":"value11"
},
{
"field_name":"value2",
"field_value":"value22"
},
...
]
这是我的代码:
public class ItemFormat
{
[JsonProperty("field_name")]
public string FieldName { get; set; }
[JsonProperty("field_value")]
public string FieldValue { get; set; }
}
查看模型(ValidationBindableBase 中有 INotifyPropertyChanged):
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()
{
}
}
后面的代码
public partial class MyViewPage : ContentPage
{
MyViewModel viewModel = new MyViewModel();
public MyViewPage ()
{
InitializeComponent ();
BindingContext = viewModel ;
}
}
XAML 视图:
<?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>
请帮助我更正 ListView 和 CarouselView 的源定义。
还有如何建立和组织视图的 XAML 代码。
我非常感谢所有帮助。谢谢。
【问题讨论】:
标签: c# listview xamarin xamarin.forms carousel