繁体   English   中英

如何在Xamarin中将数据绑定到StackLayout

[英]How to bind data to stacklayout in xamarin

我想创建垂直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>

感谢您的帮助。

根据您的Image,您只想通过ScrollView实现,如果在此视图中有很多Items是不合理的。 我建议您通过以下三种方法来实现。

  1. 旋转的Xamarin表单ListView。
  2. 自定义控件–网格+水平ScrollView
  3. 自定义渲染-Android.RecyclerView和iOS.UICollectionView(我推荐)

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

有上面的演示代码。

https://github.com/DanielCauser/XamarinHorizo​​ntalList

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

您可以使用水平的ScrollView并在内部提供一个Stacklayout ,它也是水平的。 在后面的代码中,您可以通过创建一个循环来动态创建元素,该循环遍历列表与要绑定的对象。 每当有新对象通过循环时,都会在Stacklayout内部创建一个新元素。

暂无
暂无

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

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