无法通过MVVM将ObservableCollection绑定到ListView

时间:2017-09-12 13:47:40

标签: c# xamarin mvvm xamarin.forms

在Xamarin表单上,有行数据但没有内容,只显示一行而且为空。

在模特:

 public class Item
    {
        public string kind { get; set; }
        public string etag { get; set; }
        public string id { get; set; }
        public Snippet snippet { get; set; }
        public ContentDetails contentDetails { get; set; }
        public Status status { get; set; }
        public Statistics statistics { get; set; }
    }

    public class RootObject
    {
        public string kind { get; set; }
        public string etag { get; set; }
        public string nextPageToken { get; set; }
        public PageInfo pageInfo { get; set; }
        public List<Item> items { get; set; }
    }

在ViewModel中

private ObservableCollection<TrendingModel.RootObject> _trendingRoots;
    private readonly ITrendingService _trendingService;


    public ObservableCollection<TrendingModel.RootObject> TrendingRoots
    {
        get { return _trendingRoots; }
        set
        {
            _trendingRoots = value;
            RaiseProtertyChanged(()=>TrendingRoots);
        }
    }

...

TrendingRoots = new ObservableCollection<TrendingModel.RootObject>(ridesResult);

结果是'TredingRoots' - &gt; TrendinRoot [0] .items [I]是.snippet ..... 在视图上,ListView:

 <ListView x:Name="lstHome" ItemsSource="{Binding TrendingRoots}">
        <ListView.ItemTemplate>
            <DataTemplate>
                <ViewCell>
                    <StackLayout
                           Orientation="Horizontal">

                        <Image 
                               Source="{Binding items.snippet.thumbnails.medium}"
                               Aspect="AspectFit"
                               WidthRequest="220"
                               HeightRequest="220"
                               HorizontalOptions="Center"
                               VerticalOptions="Center" />
                        <Label 
                               Text="{Binding items.snippet.title}"
                               HorizontalOptions="Center"
                               VerticalOptions="Center">

                        </Label>
                    </StackLayout>
                </ViewCell>
            </DataTemplate>
        </ListView.ItemTemplate>
    </ListView>

最终结果是listview有1个项目,此项目为空,同时,

  • 结果必须有15个项目。
  • 显示listview项目上的每个项目(当前为空,无法绑定)

1 个答案:

答案 0 :(得分:1)

<ListView x:Name="lstHome" ItemsSource="{Binding TrendingRoots[0].items}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout
                       Orientation="Horizontal">

                    <Image 
                           Source="{Binding  snippet.thumbnails.medium}"
                           Aspect="AspectFit"
                           WidthRequest="220"
                           HeightRequest="220"
                           HorizontalOptions="Center"
                           VerticalOptions="Center" />
                    <Label 
                           Text="{Binding snippet.title}"
                           HorizontalOptions="Center"
                           VerticalOptions="Center">

                    </Label>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>