简体   繁体   English

Xamarin Forms 在属性中绑定 Object

[英]Xamarin Forms Binding Object with List in property

I have the following class in my project我的项目中有以下 class

  public class ListagemVideos
    {

        public int codplaylist { get; set; }
        public string titulo { get; set; }
        public string url { get; set; }
        public string descricao { get; set; }

    }

    public class PlayList
    {
        public int codplaylist { get; set; }
        public string playlist { get; set; }
        public string descricao { get; set; }
        public ObservableCollection<ListagemVideos> videos { get; set; } = new ObservableCollection<ListagemVideos>();
    }

My XAML file我的 XAML 文件

 <AbsoluteLayout BackgroundColor="White" VerticalOptions="FillAndExpand">
                <CollectionView ItemsSource="{Binding ListaVideosCursos}">
                    <CollectionView.ItemTemplate>
                        <DataTemplate>
                            <StackLayout>
                                <Label
                                    FontFamily="titibold"
                                    FontSize="40"
                                    Text="{Binding playlist}" />
                                <Label
                                    FontFamily="titiregular"
                                    FontSize="20"
                                    Text="{Binding descricao}" />
                                <ScrollView Orientation="Horizontal">
                                    <StackLayout Margin="10">
                                        <Label
                                            x:Name="lblvideorecente"
                                            FontFamily="titiregular"
                                            FontSize="20"
                                            Text="{Binding videos.titulo}" />
                                    </StackLayout>
                                </ScrollView>
                            </StackLayout>
                        </DataTemplate>
                    </CollectionView.ItemTemplate>
                </CollectionView>
            </AbsoluteLayout>

ListaVideosCursos is the name of the ObservableCollection that receives the object's data, well my question and that of many from what I've seen around is how to bind the properties of the List video object (titulo, url, descricao) to my collectionview? ListaVideosCursos 是接收对象数据的 ObservableCollection 的名称,我的问题以及我所看到的许多问题是如何将列表视频 object(标题,url,descricao)的属性绑定到我的集合

You need to set the source videos for the StackLayout to help Label to find the value of titulo property.您需要设置StackLayout的源视频,以帮助Label找到 titulo 属性的值。

You could refer to the code below.你可以参考下面的代码。

Xaml: Xaml:

 <AbsoluteLayout BackgroundColor="White" VerticalOptions="FillAndExpand">
    <CollectionView ItemsSource="{Binding playLists}">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <StackLayout>
                    <Label
                                FontFamily="titibold"
                                FontSize="40"
                                Text="{Binding playlist}" />
                    <Label
                                FontFamily="titiregular"
                                FontSize="20"
                                Text="{Binding descricao}" />
                    <ScrollView Orientation="Horizontal">
                        <StackLayout Margin="10" BindableLayout.ItemsSource="{Binding videos}">
                            <BindableLayout.ItemTemplate>
                                <DataTemplate>
                                    <Label
                                        x:Name="lblvideorecente"
                                        FontFamily="titiregular"
                                        FontSize="20"
                                        Text="{Binding  titulo}" />
                                </DataTemplate>
                            </BindableLayout.ItemTemplate>
                            
                        </StackLayout>
                    </ScrollView>
                </StackLayout>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>
</AbsoluteLayout>

Code behind:后面的代码:

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();

        this.BindingContext = new ListagemVideosViewModel();

    }
}
public class ListagemVideosViewModel
{
    public ObservableCollection<PlayList> playLists { get; set; }
    public ListagemVideosViewModel()
    {
        playLists = new ObservableCollection<PlayList>()
        {
            new PlayList(){ codplaylist=1, descricao="A", playlist="aa", videos=new ObservableCollection<ListagemVideos>(){ new ListagemVideos() {  codplaylist=1, descricao="A2", titulo= "a_titulo", url= "A_url" } }},
            new PlayList(){ codplaylist=2, descricao="B", playlist="bb", videos=new ObservableCollection<ListagemVideos>(){ new ListagemVideos() {  codplaylist=2, descricao="B2", titulo= "b_titulo", url= "B_url" } }}

        };
    }
}
public class ListagemVideos
{

    public int codplaylist { get; set; }
    public string titulo { get; set; }
    public string url { get; set; }
    public string descricao { get; set; }

}

public class PlayList
{
    public int codplaylist { get; set; }
    public string playlist { get; set; }
    public string descricao { get; set; }
    public ObservableCollection<ListagemVideos> videos { get; set; } = new ObservableCollection<ListagemVideos>();
}

在此处输入图像描述

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

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