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

2024-05-02

  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 会拉长屏幕尺寸?

您应该将 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>

效果真的很好。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

嵌套 CollectionView 和显示(使用 Visual Studio 2019、Xamarin XPlatform Android) 的相关文章

  • 删除视图并重新创建它

    有没有办法删除设置的视图 setContentView R layout set map center mapView MapView findViewById R id mapview 如果我再次调用此视图 则会收到一条错误消息 java
  • 居中复选框视图

    如果除了 或代替 复选框之外 您还对单选按钮感兴趣 请参阅this https stackoverflow com questions 16701806 centering views 2而是提问 尽管存在
  • 在Android内存中存储gif图像

    我对安卓还很陌生 我想将图像保存到内存中 然后从内存中检索图像并将其加载到图像视图中 我已使用以下代码成功将图像存储在内存中 void saveImage String fileName img cnt jpg File file new
  • 有没有办法替代Android中的标准Log?

    有没有办法以某种方式拦截对 android 中标准 Log 的调用并执行其他操作 在桌面 Java 中 人们通常会得到一些记录器 因此有多种方法可以安装不同的日志处理程序 实现 但是 Android似乎对Log有静态调用 我找不到任何有关替
  • 无法从 com.android.aaptcompiler.ParsedResource@ef79973 提取资源

    无法从 com android aaptcompiler ParsedResource ef79973 提取资源 无法从 com android aaptcompiler ParsedResource 4c95ce87 提取资源 C Use
  • Android 性能:SharedPreferences 的成本

    当我的应用程序启动时 我使用分片首选项中的值填充容器类 这个想法是处理 SharedPreferences 和 PreferenceManager 一次 因为我猜它们很重 这是一个示例 SharedPreferences prefs Pre
  • cordova插件条码扫描仪打不开扫描

    我的条形码扫描仪插件有问题 我不是天才 我不太了解如何编写网络应用程序 我使用phonegap和cordova 并且尝试制作一个网络应用程序 在单击链接后扫描条形码 我之前已经使用此命令行安装了该插件 cordova plugin add
  • 以编程方式应用样式资源

    我没有找到一种以编程方式做到这一点的方法 所以我在这里发布这个问题 我也没有找到与此相关的任何问题 我有一个资源样式 在 res values styles xml 中定义 我想做的是使用 java 将这种样式应用到我正在操作的 View
  • popupBackground 与 Material Design 相关的问题

    我一直致力于将我的应用程序更新为 Material Design 我有一个使用选项卡的应用程序 由于某种原因 每当我使用 android popupBackground 设置下拉菜单颜色时 它就会崩溃 我设置了一个带有选项卡的默认项目并使用
  • 在尝试使用 GPS 之前如何检查 GPS 是否已启用

    我有以下代码 但效果不好 因为有时 GPS 需要很长时间 我该如何执行以下操作 检查GPS是否启用 如果启用了 GPS 请使用 GPS 否则请使用网络提供商 如果 GPS 时间超过 30 秒 请使用网络 我可以使用时间或 Thread sl
  • 与 Dagger 一起使用时,Espresso 生成 FileNotFoundException

    我一直在研究旧版 Android 应用程序 尝试为其添加测试和适当的架构 该应用程序有一个主要LaunchActivity它在启动时运行一系列检查 最初 该活动使用 Dagger 来 注入依赖项 活动将使用它来运行检查 但效果相当糟糕 我转
  • 确定视图是否在屏幕上 - Android

    我对这个有点困惑 首先也是最重要的是 以下链接很有用 但是我提出了一些可见性问题 链接 检查视图可见性 https stackoverflow com questions 4628800 android how to check if a
  • 在 Android ADT Eclipse 插件中滚动布局编辑器

    有谁知道当布局编辑器的内容溢出一个 屏幕 时如何滚动这些内容 我说的是在设计时使用 ADT 布局编辑器 而不是在物理设备上运行时滚动 效果很好 关闭 Android 布局编辑器中的剪辑 切换剪辑 按钮位于 Android 布局编辑器的右上角
  • BadPaddingException:无效的密文

    我需要一些帮助 因为这是我第一次编写加密代码 加密代码似乎工作正常 但解密会引发错误 我得到的错误是 de flexiprovider api exceptions BadPaddingException 无效的密文 in the 解密函数
  • 获取当前图片在图库中显示的位置

    在我的应用程序中 我有一个图片库 但我想检测当前显示图像的位置 例如 当我启动我的活动时 位置是 0 但是当我在图库中滚动时 我想获取当前显示图像的位置 我尝试过 OnFocusChanged OnItemClicked 但只有当我单击图库
  • 从多个 TextView 中选择文本

    如何在android中从多个文本视图中选择文本 我已经尝试过以下代码 该代码一次仅适用于一个文本视图 我想一次性从许多文本视图中复制文本 android textIsSelectable true 你不能同时这样做 您需要在单个文本视图中设
  • 在上下文操作模式下选择时,ListView 项目不会在视觉上“突出显示”

    我关注了 Android 官方网站创建上下文操作菜单的教程 http developer android com guide topics ui menus html CAB 使用下面的代码 当我长按我的 ListView 项目之一时 它确
  • SQLiteDatabase.openDatabase 与 SQLiteOpenHelper.getReadableDatabase

    这两种方法有什么区别吗 两者都返回一个打开的 SQLiteDatabase 如果数据库不存在 两者都可以创建数据库 当需要读 写时 SQLiteOpenHelper 还具有 getWriteableDatabase 我应该使用哪种方法以及在
  • 画布:尝试使用回收的位图错误

    我是一个相当新的程序员 所以任何建议将不胜感激 我有一个类 每次调用它时都会在循环中运行 AsyncTask AsyncTask 看起来像这样 public class LoadImageTask extends AsyncTask
  • 我可以通过在 Android Activity 中声明适当的成员“静态”来提高效率吗

    如果一个 Activity 在实践中是单例 我认为我可以通过声明适当的成员 静态 来获得一些效率 且风险为零 是的 The Android 文档说 http developer android com guide topics fundam

随机推荐