Xamarin 表单绑定 Object 与 属性 中的列表

Xamarin Forms Binding Object with List in property

我的项目中有以下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>();
    }

我的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 是接收 object 数据的 ObservableCollection 的名称,我的问题以及我所看到的许多人的问题是如何绑定视频列表的属性 object(标题,url,描述)到我的收藏视图?

您需要设置 StackLayout 的源视频以帮助 Label 找到 titulo 属性 的值。

您可以参考下面的代码。

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>

后面的代码:

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>();
}