【问题标题】:How to bind data to stacklayout in xamarin如何将数据绑定到xamarin中的stacklayout
【发布时间】:2019-02-18 22:27:21
【问题描述】:

我想创建垂直的 ScrollView。 像这样:

这是我尝试过的代码:

<ScrollView x:Name="newArrival" Orientation="Horizontal" HorizontalScrollBarVisibility="Always">
    <StackLayout Orientation="Horizontal">
        <Frame BackgroundColor="Black" HeightRequest="100" WidthRequest="100">
            <StackLayout>
                <Image HeightRequest="70"/>
                <Label Text="{Binding Name}" FontSize="15" HorizontalOptions="Center"/>
                <Label Text="{Binding Desc}" FontSize="13" HorizontalOptions="Center"/>
            </StackLayout>
        </Frame>
    </StackLayout>
</ScrollView>

感谢您的帮助。

【问题讨论】:

标签: c# xaml xamarin


【解决方案1】:

根据你的Image,你只想通过ScrollView来实现,如果这个视图中有很多Item是不合理的。我建议你通过以下三种方式来实现。

  1. 旋转的 Xamarin 表单 ListView。
  2. 自定义控件 - 网格 + 水平滚动视图
  3. 自定义渲染 - Android.RecyclerView & iOS.UICollectionView(我推荐它)

如果您想了解更多关于三种方式的详细信息,可以参考此链接。 https://causerexception.com/2018/02/06/xamarin-forms-ultimate-horizontal-list-guide/

上面有demo的代码。

https://github.com/DanielCauser/XamarinHorizontalList

如果您对 Model-View-ViewModel (MVVM) 的数据绑定有疑问,请参考此链接。 https://blog.xamarin.com/introduction-to-data-binding/

【讨论】:

    【解决方案2】:

    您可以使用水平方向的ScrollView 并在内部设置Stacklayout,它也是水平方向的。 在代码隐藏中,您可以通过创建一个循环来动态创建元素,该循环在您的列表中运行您想要绑定的对象。 每当一个新对象通过循环时,就会在 Stacklayout 中创建一个新元素。

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2021-03-03
      • 1970-01-01
      • 1970-01-01
      • 2018-07-14
      • 1970-01-01
      • 2017-05-10
      • 2021-10-02
      • 2019-01-18
      相关资源
      最近更新 更多