【问题标题】:Use Xamarin.Forms Carousel View to display List of ListView, each contains list of objects使用 Xamarin.Forms Carousel View 显示 ListView 列表,每个包含对象列表
【发布时间】:2019-07-07 12:08:48
【问题描述】:

我正在使用 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 代码。

我非常感谢所有帮助。谢谢。

【问题讨论】:

    标签: c# listview xamarin xamarin.forms carousel


    【解决方案1】:

    你到底得到了什么错误?您是否尝试过将 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-17
      相关资源
      最近更新 更多