简体   繁体   English

嵌套的 CollectionViews 和显示(使用 Visual Studio 2019、Xamarin XPlatform Android)

[英]Nested CollectionViews and display (with Visual Studio 2019, Xamarin XPlatform Android)

  1. Nested CollectionView with scrolling one inside another: is it officially supported?嵌套的 CollectionView 滚动一个内部另一个:它是否得到官方支持?
  2. Display these collections problem显示这些集合问题

See below my data model and the XAML code (I have no site to put the resulting screen image in)请参阅下面我的数据模型和 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>

snapshot of outer & inner collectionviews:外部和内部集合视图的快照:

在此处输入图片说明

I accented the colors to better see the layouts not shrinked.我强调了颜色,以便更好地看到没有缩小的布局。

Questions : (I have tried multiple configurations but without solution)问题:(我尝试了多种配置但没有解决方案)

  1. how can I shrink the StackLayouts to its contents?如何将 StackLayouts 缩小到其内容?
  2. why the StackLayouts elongate greater the size of the screen?为什么 StackLayouts 会拉长更大的屏幕尺寸?

Instead of using CollectionView, you should use StackLayout with the BindableLayout.您应该将 StackLayout 与 BindableLayout 一起使用,而不是使用 CollectionView。 I just had the same issue and finally I resolved using the BindableLayout inside of a StackLayout as follows:我刚刚遇到了同样的问题,最后我使用 StackLayout 中的 BindableLayout 解决了如下问题:

在此处输入图片说明

To give a better idea, I have some list of objects inside a parent.为了给出更好的主意,我在父对象中有一些对象列表。 Something like this:像这样的东西:

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

So I had a List of Customers that contains a List of Product where each one contains a List of Batches and finally they have a List of Serials.所以我有一个客户列表,其中包含一个产品列表,其中每个都包含一个批次列表,最后他们有一个序列列表。

<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>

It's working really good.它工作得很好。

在此处输入图片说明

Nested CollectionView with scrolling one inside another : is it officially supported ?嵌套的 CollectionView 在另一个里面滚动:它是否得到官方支持?

In my knowledge, nested CollectionViews are not supported by all platforms as they have a Scroll in them and Nested ScrollViews are a widely known bad practice.据我所知,并非所有平台都支持嵌套的CollectionViews ,因为它们中有一个 Scroll 并且嵌套的ScrollViews是众所周知的不良做法。

Display these collections problem显示这些集合问题

I was not able to understand what exactly did you mean here but if you could clarify, maybe I would be able to help you out.我无法理解您在这里的确切含义,但如果您能澄清一下,也许我可以帮助您。

How can I shrink the StackLayouts to its contents?如何将StackLayouts缩小到其内容?

By setting the Spacing to 0 would be a good start!通过将Spacing设置为 0 将是一个好的开始!

I expect StackLayouts no longer than the size of the screen as it is with the code above and a few data我希望 StackLayouts 不会超过屏幕的大小,因为它是上面的代码和一些数据

Is it that your StackLayout taking the whole screen?是你的 StackLayout 占据了整个屏幕吗? (The one which is in the ItemTemplate ?) (在ItemTemplate中的那个?)

I was just dealing with this myself, and found this solution: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/grouping我自己只是在处理这个问题,并找到了这个解决方案: https : //docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/collectionview/grouping

In short: The idea is to restructure your data in such a way that the parent is a List<child> with additional properties, turn IsGrouping on in your CollectionView , structure the parent element as the CollectionView.GroupHeaderTemplate , and the child as the CollectionView.ItemTemplate .简而言之:这个想法是以这样的方式重构你的数据,父元素是一个List<child>具有附加属性,在你的CollectionView打开IsGrouping ,将父元素构造为CollectionView.GroupHeaderTemplate ,将子元素构造为CollectionView.ItemTemplate

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

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