简体   繁体   English

当我尝试在 Xamarin Forms

[英]Only the first item in a list is rendered when I try to output a collectionview inside a collectionview in Xamarin Forms

I'm trying to render a complex object in a list in a Xamarin view.我正在尝试在 Xamarin 视图的列表中呈现复杂的 object 。 the object I'm trying to list out is a collection of viewmodels.我要列出的 object 是视图模型的集合。 One of the properties of each of these viewmodels is another collection.每个视图模型的属性之一是另一个集合。

To illustrate, the list contains the following members:为了说明,该列表包含以下成员:

public class ItemsViewModel : BaseViewModel
    {
        public int Id { get; private set; }
        public bool UsedOption1 { get; private set; }
        public bool UsedOption2 { get; private set; }
        public bool UsedOption3 { get; private set; }
        public bool UsedOption4 { get; private set; }
        public List<ActivityViewModel> Activities { get; private set; }
    }

The List looks like this:列表如下所示:

public class ActivityViewModel
{
    public string LocationDisplayName { get; private set; }
    public int SwimmersEaten { get; private set; }
}

The ActivityViewModel could have 4 items at most. ActivityViewModel 最多可以有 4 个项目。 If I use the following code to render the Viewmodel:如果我使用以下代码来呈现 Viewmodel:

<RefreshView IsRefreshing="{Binding IsBusy, Mode=TwoWay}" Command="{Binding LoadItemsCommand}">
        <CollectionView x:Name="ItemsCollectionView"
                ItemsSource="{Binding ItemsViewModel}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="10">
                        <Label Text="{Binding Title}"/>
                        <CollectionView x:Name="Actions"
                            ItemsSource="{Binding Activities}">
                            <CollectionView.ItemTemplate>
                                <DataTemplate>
                                    <StackLayout Padding="10" Orientation="Horizontal">
                                        <Label Text="{Binding DisplayName}"/>
                                        <Label Text="{Binding NumberOfGroups}"/>
                                    </StackLayout>
                                </DataTemplate>
                            </CollectionView.ItemTemplate>
                        </CollectionView>
                        <Label Text="Used Option 1"
                                IsVisible="{Binding UsedOption1}"/>
                        <Label Text="Used Option 2"
                                IsVisible="{Binding UsedOption2}"/>
                        <Label Text="Used Option 3"
                                IsVisible="{Binding UsedOption3}"/>
                        <Label Text="Used Option 4"
                                IsVisible="{Binding UsedOption4}"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView> 

I get the first item in the list rendered almost as I want, albeit missing the used options on the end.我得到了几乎按照我想要的方式呈现的列表中的第一项,尽管最后缺少使用的选项。 My rendering looks something like:我的渲染看起来像:

Title 1
  a 0
  b 2
  c 0
  d 1

I get the title of the first top level item, the display name and number of groups for each of the 4 items within.我得到了第一个顶级项目的标题、显示名称和其中 4 个项目中每个项目的组数。 The trouble is there are subsequent items in my list that aren't appearing.问题是我的列表中有后续项目没有出现。 I can only get items beyond the first in my list to show if I chop out the lister in the middle, like this:如果我在中间切掉列表器,我只能获取列表中第一个之外的项目,如下所示:

<RefreshView IsRefreshing="{Binding IsBusy, Mode=TwoWay}" Command="{Binding LoadItemsCommand}">
        <CollectionView x:Name="ItemsCollectionView"
                ItemsSource="{Binding ItemsViewModel}">
            <CollectionView.ItemTemplate>
                <DataTemplate>
                    <StackLayout Padding="10">
                        <Label Text="{Binding Title}"/>
                        <Label Text="Used Option 1"
                                IsVisible="{Binding UsedOption1}"/>
                        <Label Text="Used Option 2"
                                IsVisible="{Binding UsedOption2}"/>
                        <Label Text="Used Option 3"
                                IsVisible="{Binding UsedOption3}"/>
                        <Label Text="Used Option 4"
                                IsVisible="{Binding UsedOption4}"/>
                    </StackLayout>
                </DataTemplate>
            </CollectionView.ItemTemplate>
        </CollectionView>
    </RefreshView> 

This renders the title and options for all the viewmodels in my list, like this:这会呈现我列表中所有视图模型的标题和选项,如下所示:

Title 1
Used Option 3

Title 2
Used Option 2
Used Option 3

I can see the presence of the inner collectionview is doing something odd to prevent the rendering of items beyond its listing, preventing the resumption of rendering the contents of the outer list.我可以看到内部集合视图的存在正在做一些奇怪的事情,以防止呈现超出其列表的项目,从而防止恢复呈现外部列表的内容。 I'm definitely missing something but can't figure it out.我肯定错过了一些东西,但无法弄清楚。 How can I work around this apparent limitation?我该如何解决这个明显的限制?

I couldn't find an answer that let me render nested lists, which Jason said to be impossible in a comment.我找不到让我渲染嵌套列表的答案,Jason 在评论中说这是不可能的。

Since I know my ViewModel will only need to render a list of no more than 4 items, my soluton was to add the following members to ItemsViewModel:由于我知道我的 ViewModel 只需要呈现不超过 4 个项目的列表,我的解决方案是将以下成员添加到 ItemsViewModel:

public ActivityViewModel Action1 => Activities?.FirstOrDefault();
public ActivityViewModel Action2 => Activities?.Skip(1).FirstOrDefault();
public ActivityViewModel Action3 => Activities?.Skip(2).FirstOrDefault();
public ActivityViewModel Action4 => Activities?.Skip(3).FirstOrDefault();

And my view now looks like this:我的观点现在看起来像这样:

<RefreshView IsRefreshing="{Binding IsBusy, Mode=TwoWay}" Command="{Binding LoadItemsCommand}">
    <CollectionView x:Name="ItemsCollectionView"
            ItemsSource="{Binding ItemsViewModel}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout Orientation="Horizontal">
                    <Label Text="{Binding Title}"/>
                    <Label Text="{Binding Action1.DisplayName}"/>
                    <Label Text="{Binding Action1.NumberOfGroups}"/>
                    <Label Text="{Binding Action2.DisplayName}"/>
                    <Label Text="{Binding Action2.NumberOfGroups}"/>
                    <Label Text="{Binding Action3.DisplayName}"/>
                    <Label Text="{Binding Action3.NumberOfGroups}"/>
                    <Label Text="{Binding Action4.DisplayName}"/>
                    <Label Text="{Binding Action4.NumberOfGroups}"/>
                    <Label Text="Used Option 1"
                        IsVisible="{Binding UsdOption1}"/>
                    <Label Text="Used Option 2"
                        IsVisible="{Binding UsdOption2}"/>
                    <Label Text="Used Option 3"
                        IsVisible="{Binding UsdOption3}"/>
                    <Label Text="Used Option 4"
                        IsVisible="{Binding UsdOption4}"/>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</RefreshView> 

Not an ideal solution because of duplicate entries in the view's data template, but an effective workaround.由于视图的数据模板中有重复条目,这不是一个理想的解决方案,而是一种有效的解决方法。

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

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