简体   繁体   English

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

[英]Use Xamarin.Forms Carousel View to display List of ListView, each contains list of objects

im using Xamarin.Forms and im having a problem trying to use alexrainman's Carousel View . 我正在使用Xamarin.Forms,并且在尝试使用alexrainman的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. 特别是,我想设置一个旋转木马视图,其中每个视图都是一个ListView,然后每个ListView将显示一个对象列表。

I have read through the document pages and define the sources for my CarouselView and ListView as follow: 我已经阅读了文档页面,并定义了CarouselViewListView的源,如下所示:

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: 我的ListView将具有对象列表,如下所示:

[
{
"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): 视图模型(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()
        {
        }
    }

code behind 背后的代码

public partial class MyViewPage : ContentPage
    {
        MyViewModel viewModel = new MyViewModel();
        public MyViewPage ()
        {
            InitializeComponent ();
            BindingContext = viewModel ;
        }
    }

The XAML view: 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>

Please help me to correct the definition of the sources for my ListView and CarouselView. 请帮助我更正ListView和CarouselView的源定义。

Also how to establish and organize the view's XAML code. 以及如何建立和组织视图的XAML代码。

I really appreciate all help. 我真的很感谢所有帮助。 Thank you. 谢谢。

What exactly do you get as an error? 您究竟会得到什么错误呢? Have you tried wrapping your ListView in some root layout like ContentView or StackLayout. 您是否尝试过将ListView包装在某些根布局中,例如ContentView或StackLayout。

Code-behind and ViewModel implementation both look okay, so I guess it is only problem with parsing the XAML. 代码隐藏和ViewModel实现看起来都不错,所以我想这只是解析XAML的问题。

So, try to do something like this with your XAML view: 因此,请尝试使用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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM