【问题标题】:Nested CollectionViews and display (with Visual Studio 2019, Xamarin XPlatform Android)嵌套的 CollectionViews 和显示(使用 Visual Studio 2019、Xamarin XPlatform Android)
【发布时间】:2019-09-11 06:10:13
【问题描述】:
  1. 嵌套的 CollectionView 相互滚动:官方支持吗?
  2. 显示这些集合问题

请看下面我的数据模型和 XAML 代码(我没有网站可以放入生成的屏幕图像)

namespace Notes.Models
{
    public class Note
    {
        public enum NoteStatus { suspended, alive }

        public string       Description { get; set; }
        public NoteStatus   Status { get; set; }
    }

    public class NotesContainer
    {
        public string                       Name { get; set; }
        public DateTime                     LastModified { get; set; }
        public ObservableCollection<Note>   ListOfNotes { get; set; }
    }
}
  <CollectionView x:Name="notesContainers" SelectionMode="Single" EmptyView="No items currently exist !">
    <CollectionView.ItemTemplate>
      <DataTemplate>
        <Frame BorderColor="Red" BackgroundColor="Beige" CornerRadius="3" HasShadow="False" Padding="5">
          <StackLayout BackgroundColor="Aqua" Padding="5">
            <Grid>
              <Grid.RowDefinitions><RowDefinition Height="auto"/><RowDefinition Height="auto"/></Grid.RowDefinitions>
              <Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
              <Label Grid.RowSpan="2" Text="{Binding Name}" VerticalTextAlignment="Center" FontSize="Large"/>
              <Label Grid.Row="0" Grid.Column="1" Text="{Binding LastModified, StringFormat='\{0:dddd dd}'}" HorizontalTextAlignment="End"/>
              <Label Grid.Row="1" Grid.Column="1" Text="{Binding LastModified, StringFormat='\{0:MMMM yyyy}'}" HorizontalTextAlignment="End"/>
            </Grid>
            <StackLayout BackgroundColor="BlueViolet" Padding="10">
              <CollectionView ItemsSource="{Binding ListOfNotes}" SelectionMode="Single" EmptyView="No items currently exist !">
                <CollectionView.ItemTemplate>
                  <DataTemplate>
            <StackLayout BackgroundColor="Coral" Padding="0,3">
                    <Frame BorderColor="Blue" BackgroundColor="LightBlue" CornerRadius="3" HasShadow="False" Padding="5">
                      <StackLayout Orientation="Horizontal">
                        <Label Text="{Binding Description}" HorizontalOptions="Start" VerticalTextAlignment="Center"/>
                        <Label Text="{Binding Status}" HorizontalOptions="EndAndExpand" VerticalTextAlignment="Center" HorizontalTextAlignment="End"/>
                      </StackLayout>
                    </Frame>
             </StackLayout>
                 </DataTemplate>
                </CollectionView.ItemTemplate>
              </CollectionView>
            </StackLayout>
          </StackLayout>
        </Frame>
      </DataTemplate>
    </CollectionView.ItemTemplate>
  </CollectionView>

外部和内部集合视图的快照:

我强调了颜色以更好地查看未缩小的布局。

问题:(我尝试了多种配置但没有解决方案)

  1. 如何将 StackLayouts 缩小到其内容?
  2. 为什么 StackLayouts 会拉长更大的屏幕尺寸?

【问题讨论】:

  • 你的 CollectionView 无法显示,因为你没有设置外部 CollectionView 的 ItemSource,如果你嵌套 CollectionView 并在另一个里面滚动,很多时候它会导致滑动冲突
  • 外部 CollectionView 的 ItemsSource 是在 XAML 中所见的 x:Name notesContainers 后面的代码中设置的。数据显示正确。问题是 Frames 和/或 StackLayouts 不会缩小到它们的内容。每个项目都沿着屏幕延伸等等。
  • 你有没有考虑过使用ListView Grouping,你的需求看起来像两个list,一个是parent,一个是child。参考:docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/…
  • 我第一次看到 ListView 但论坛说嵌套 ListView 存在嵌套滚动姿势问题。因此我希望新的 CollectionView 能够接受嵌套的。这是可能的,因为我看到许多网页在页面本身滚动的内部滚动代码区域滚动!不过,我会回顾一下 DataTemplates 中的布局和 ListView 中的 Group Headers 的可能性。
  • 可以,只需要在listview中显示第二个list数据,listview分组就可以实现

标签: android xaml xamarin.forms collectionview


【解决方案1】:

嵌套的 CollectionView 并在另一个中滚动:官方支持吗?

据我所知,并非所有平台都支持嵌套CollectionViews,因为它们中有一个滚动条,而嵌套ScrollViews 是一种广为人知的不良做法。

显示这些集合问题

我无法理解你在这里的确切意思,但如果你能澄清一下,也许我可以帮助你。

如何将StackLayouts 缩小为其内容?

Spacing 设置为 0 将是一个好的开始!

我希望 StackLayouts 不会超过屏幕大小,就像上面的代码和一些数据一样

是你的 StackLayout 占据了整个屏幕吗? (ItemTemplate中的那个?)

【讨论】:

  • 1. & 2.没有嵌套我怎么能有以下显示:每个外部项目,其内部项目在其下方缩进?并且紧接着下一个外部......在每个外部项目之前没有一个长长的空屏幕(就像我现在一样)? 3. Spacing="0" 不起作用(外部项之间总是长空背景颜色区域) 4. 是的,第一个 StackLayout
  • 上传描述的图片可能会让我更容易!
  • 我在上面的第一条消息中添加了屏幕图像
  • 我想让你添加你想要实现的东西而不是你拥有的东西!!
  • 我希望第一个具有 BackgroundColor="Aqua" 的 StackLayout 将其大小调整为它的内容,并且在具有 BackgroundColor="BlueViolet" 的第二个 ColllectionView 中的第二个 StackLayout 也调整大小(围绕布局 Repas 和 Oeufs图片上)希望对您有所帮助。
【解决方案2】:

我自己只是在处理这个问题,并找到了这个解决方案: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/grouping

简而言之:这个想法是重组您的数据,使父元素是具有附加属性的List&lt;child&gt;,在您的CollectionView 中打开IsGrouping,将父元素构造为CollectionView.GroupHeaderTemplate,和孩子作为CollectionView.ItemTemplate

【讨论】:

  • 谢谢。我对 CollectionView 有其他问题。因此,我按照您的建议重用了带有分组的 ListView,现在我的应用程序几乎可以按照我的意愿运行。
【解决方案3】:

您应该将 StackLayout 与 BindableLayout 一起使用,而不是使用 CollectionView。 我刚刚遇到了同样的问题,最后我使用 StackLayout 中的 BindableLayout 解决了如下问题:

为了更好地理解,我在父对象中有一些对象列表。像这样的:

Customers (Items in the code)
--- Products
------- Batches
-----------Series

所以我有一个客户列表,其中包含一个产品列表,其中每个都包含一个批次列表,最后他们有一个序列列表。

<ScrollView>
    <StackLayout BindableLayout.ItemsSource="{Binding Items}">
        <BindableLayout.ItemTemplate>
            <DataTemplate>
                <StackLayout Orientation="Vertical" HorizontalOptions="FillAndExpand">

                    <StackLayout x:DataType="ms:Customer" Orientation="Vertical">

                        <StackLayout BindableLayout.ItemsSource="{Binding Products}">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>

                                    <Frame CornerRadius="10" Margin="5, 5, 5, 5" HasShadow="True" BackgroundColor="#5ac8fa">
                                        <StackLayout>

                                            <StackLayout x:DataType="ms:Product" Orientation="Vertical">
                                                <Label Text="{Binding Name}" FontAttributes="Bold" FontSize="Caption" />
                                                <Label Text="{Binding ProductCode}" FontAttributes="Italic" FontSize="Micro" />

                                                <StackLayout BindableLayout.ItemsSource="{Binding Batches}">
                                                    <BindableLayout.ItemTemplate>
                                                        <DataTemplate>
                                                            <StackLayout x:DataType="ms:Batch" Orientation="Vertical" HorizontalOptions="FillAndExpand">

                                                                <Grid Margin="0,10,0,0">
                                                                    <Grid.ColumnDefinitions>
                                                                        <ColumnDefinition Width="2*" />
                                                                        <ColumnDefinition Width="1*" />
                                                                    </Grid.ColumnDefinitions>
                                                                    <Label Grid.Column="0" Text="{Binding Code}" FontAttributes="Bold" FontSize="Caption" />
                                                                    <Label Grid.Column="1" Text="{Binding ExpiredDateFormated}" FontAttributes="Italic" HorizontalTextAlignment="End" FontSize="Micro" />
                                                                </Grid>

                                                                <StackLayout BindableLayout.ItemsSource="{Binding Serials}">
                                                                    <BindableLayout.ItemTemplate>
                                                                        <DataTemplate>
                                                                            <StackLayout x:DataType="ms:Serial" Orientation="Vertical" HorizontalOptions="FillAndExpand">
                                                                                <Frame CornerRadius="10" HasShadow="True">

                                                                                    <StackLayout>
                                                                                        <Label Text="{Binding SerialCode}" FontAttributes="Bold" FontSize="Micro" />
                                                                                    </StackLayout>
                                                                                    <Frame.GestureRecognizers>
                                                                                        <TapGestureRecognizer 
                                                                                                            NumberOfTapsRequired="1" 
                                                                                                            Command="{Binding Source={RelativeSource AncestorType={x:Type vm:SearchSerialsByLocationViewModel}}, Path=SerialTappedCommand}" 
                                                                                                            CommandParameter="{Binding .}" />
                                                                                    </Frame.GestureRecognizers>
                                                                                </Frame>
                                                                            </StackLayout>
                                                                        </DataTemplate>
                                                                    </BindableLayout.ItemTemplate>
                                                                </StackLayout>

                                                            </StackLayout>
                                                        </DataTemplate>
                                                    </BindableLayout.ItemTemplate>
                                                </StackLayout>

                                            </StackLayout>

                                        </StackLayout>
                                    </Frame>

                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                        </StackLayout>

                    </StackLayout>
                </StackLayout>
            </DataTemplate>
        </BindableLayout.ItemTemplate>
    </StackLayout>
</ScrollView>

效果很好。

【讨论】:

  • 非常感谢。我从来没有想过你的解决方案。我会试试的。
  • 我在尝试此解决方案时收到错误“属性 'DataTemplateContent' 设置不止一次”。
  • 非常感谢!尝试实现类似的功能让我省去了很多麻烦!
猜你喜欢
  • 1970-01-01
  • 2023-03-14
  • 1970-01-01
  • 1970-01-01
  • 2019-12-10
  • 2019-07-21
  • 2020-04-11
  • 1970-01-01
  • 2022-10-24
相关资源
最近更新 更多