Xamarin 嵌套几个 CollectionView

Xamarin nesting few CollectionViews

我有 3 个 类,第一个是 activity,第二个是饭菜,第三个是配料。 我想首先显示这些嵌套的 CollectionViews,显示活动、下一餐和最后的成分。我仍然收到“属性 'ElementTemplateContent' 设置了不止一次。”。 前两个 CollectionViews 工作正常。当我尝试添加第三个时出现问题。 我还尝试添加 Bindable 布局而不是 CollectionView,但出现了同样的错误。

<CollectionView Margin="15, 30, 15, 15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" SelectionMode="None" ItemsSource="{Binding Meal}" IsVisible="{Binding ActType}">
    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <Label TextColor="#2F3246" FontSize="12">
                <Label.FormattedText>
                    <FormattedString>
                        <FormattedString.Spans>
                            <Span Text="{Binding Name}" FontAttributes="Bold"/>
                        </FormattedString.Spans>
                    </FormattedString>
                </Label.FormattedText>
            </Label>
            <CollectionView Margin="15, 30, 15, 15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" SelectionMode="None" ItemsSource="{Binding Ingradients}">
                <CollectionView.ItemsLayout>
                    <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Label TextColor="#2F3246" FontSize="12">
                            <Label.FormattedText>
                                <FormattedString>
                                    <FormattedString.Spans>
                                        <Span Text="{Binding Name}" FontAttributes="Bold"/>
                                    </FormattedString.Spans>
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
            </CollectionView>
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>

这是对象的样子

new Activity {
    Name = "Breakfast",
    Color = "#498467",
    // Color = "#C5283D",
    ActType = true,
    Time = "07:00",
    Meal = new ObservableCollection<Meal>{
        new Meal
        {
            Name = "Oatmeal",
            Ingradients = new ObservableCollection<Ingradients>
            {
                new Ingradients
                {
                    Name = "Oat",
                    Amount = 100.0,
                    Calories = 200,
                    Carbo = 120.0,
                    Protein = 20.0,
                    Fat = 10.0
                },
                new Ingradients
                {
                    Name = "Milk",
                    Amount = 100.0,
                    Calories = 20,
                    Carbo = 20.0,
                    Protein = 20.0,
                    Fat = 5.0
                }
            }
        }
    }
}

某些类型的容器(例如DataTemplate)只能包含一个child。您应该将 DataTemplate 内容包装成单个 object,例如 StackLayout:

<CollectionView Margin="15, 30, 15, 15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" SelectionMode="None" ItemsSource="{Binding Meal}" IsVisible="{Binding ActType}">
    <CollectionView.ItemsLayout>
        <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
    </CollectionView.ItemsLayout>
    <CollectionView.ItemTemplate>
        <DataTemplate>
            <StackLayout Orientation="Vertical"> // <--------------------------
              <Label TextColor="#2F3246" FontSize="12">
                <Label.FormattedText>
                    <FormattedString>
                        <FormattedString.Spans>
                            <Span Text="{Binding Name}" FontAttributes="Bold"/>
                        </FormattedString.Spans>
                    </FormattedString>
                </Label.FormattedText>
              </Label>
              <CollectionView Margin="15, 30, 15, 15" HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand" SelectionMode="None" ItemsSource="{Binding Ingradients}">
                <CollectionView.ItemsLayout>
                    <LinearItemsLayout Orientation="Vertical" ItemSpacing="1"/>
                </CollectionView.ItemsLayout>
                <CollectionView.ItemTemplate>
                    <DataTemplate>
                        <Label TextColor="#2F3246" FontSize="12">
                            <Label.FormattedText>
                                <FormattedString>
                                    <FormattedString.Spans>
                                        <Span Text="{Binding Name}" FontAttributes="Bold"/>
                                    </FormattedString.Spans>
                                </FormattedString>
                            </Label.FormattedText>
                        </Label>
                    </DataTemplate>
                </CollectionView.ItemTemplate>
              </CollectionView>
            </StackLayout> // <--------------------------
        </DataTemplate>
    </CollectionView.ItemTemplate>
</CollectionView>