【问题标题】:Xamarin Forms - Implementing a nested listXamarin Forms - 实现嵌套列表
【发布时间】:2018-02-16 22:45:29
【问题描述】:

我有一个详细信息列表(篮子),每个详细信息中都有另一个列表(水果)。我想显示这些细节,我首先想到的是 ListView 中的 ListView。但是在查看建议时,给了我诸如 thisthis 这样的结果,这主要表明在 Xamarin Forms 中实现并不是一个好主意。

目前,我使用 FreshMvvM 作为我的 MvvM 框架。至于我要显示的数据,我有一组篮子,每个篮子都有几个水果。我希望也显示那些属于特定篮子的水果的图像。请参考图片。

我想知道这个或其他方面是否有改进,关于如何实现我的列表或任何其他方式来实现上述行为的布局的任何其他想法。谢谢。

到目前为止我的代码:
XAML:

<ListView ItemsSource="{Binding Baskets}" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding BasketID}" />
                    <ImageCell 
                        Text="{Binding FruitID}" 
                        Detail="{Binding FruitName}" 
                        ImageSource="{Binding ImageURL}">
                    </ImageCell>
                </StackLayout>
             </ViewCell>
         </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

类:

public class Basket
{
    public string BasketID{ get; set; }
    public string BasketName{ get; set; }
}

public class Fruit
{
    public string FruitID{ get; set; }
    public string FruitName{ get; set; }
    public string ImageURL{ get; set; }
}

【问题讨论】:

  • 你可能必须实现一个自定义渲染器,因为它在 XF 中的实现方式,嵌套列表至少会在 Android 上崩溃。
  • @PaulKertscher 是的,我希望应用程序的崩溃尽可能少。如果可以,请向我展示一些有助于我创建自定义渲染器的指南/链接。谢谢。
  • 请参阅developer.xamarin.com/guides/xamarin-forms/… - 有很多关于自定义渲染器的信息。不幸的是,除此之外我无法为您提供太多帮助,因为我放弃了使用嵌套 ListViews 并以另一种方式解决了我的问题。
  • 感谢@PaulKertscher 提供的信息。另外,如果我可能会问,除了使用嵌套的 ListViews 之外,还有哪些其他可能的方式来实现上述行为?

标签: c# xamarin mvvm xamarin.forms freshmvvm


【解决方案1】:

您可以使用ListView 来遍历购物篮集合,同时使用像ItemsControl 这样的自定义控件来遍历水果集合。

它基本上是一个自定义控件,允许您通过ItemsSourceItemTemplate 属性将动态子支持添加到StackLayout。您可以按原样使用控件 - 除非您需要在其 StackLayout 上的 line 上将 Orientation 属性设置为 Horizontal

示例用法:

<ListView ItemsSource="{Binding Baskets}" HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout>
                    <Label Text="{Binding BasketID}" />

                    <local:ItemsControl ItemsSource="{Binding Fruits}">
                        <local:ItemsControl.ItemTemplate>
                            <DataTemplate>
                                <Image Source="{Binding ImageURL}" />
                            </DataTemplate>
                        </local:ItemsControl.ItemTemplate>
                    </local:ItemsControl>
                </StackLayout>
             </ViewCell>
         </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

注意:ItemsControl 适合仅用于小型集合,因为虚拟化/回收支持不是很复杂。我假设,由于布局是水平的,Fruits 集合中的项目数量会相对较少。

【讨论】:

  • 感谢您的详细解答。我会试试这个并检查。
  • 你能解释一下我应该使用local 指的是什么吗?抱歉这个愚蠢的问题
  • 所以我假设 - 您已经下载并添加了 ItemsControl 类到您的项目中。现在,您必须声明您使用的命名空间 - developer.xamarin.com/guides/xamarin-forms/xaml/namespaces/…
  • 谢谢。有效。但是,我想知道是否可以将&lt;Image Source="{Binding ImageURL}" /&gt; 替换为其他元素。例如,另一个StackLayoutTextCellImageCell,因为使用这些元素不会在应用中显示任何内容
  • 是的 - 您可以将其替换为您可以添加到 StackLayout 的任何元素
猜你喜欢
  • 2016-01-18
  • 1970-01-01
  • 2023-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-29
相关资源
最近更新 更多