使用Xamarin.Forms轮播视图显示ListView的列表,每个列表包含对象列表

时间:2019-07-07 12:08:48

标签: c# listview xamarin xamarin.forms carousel

我正在使用Xamarin.Forms,但在尝试使用alexrainman的Carousel View时遇到了问题。

特别是,我想设置一个“旋转木马视图”,其中每个视图都是一个ListView,然后每个ListView将显示一个对象列表。

我已通读文档页面,并为我的CarouselViewListView定义了来源,如下所示:

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代码。

我真的很感谢所有帮助。谢谢。

1 个答案:

答案 0 :(得分:0)

作为错误您会得到什么?您是否尝试过将ListView包装在某些根目录布局中,例如ContentView或StackLayout。

代码隐藏和ViewModel实现看起来都不错,所以我认为这只是解析XAML的问题。

因此,请尝试使用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>
                <ContentView>
                    <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>
                </ContentView>
            </DataTemplate>
        </controls:CarouselViewControl.ItemTemplate>
    </controls:CarouselViewControl>
</ContentPage>