Syncfusion Xamarin Listview不显示任何项目

时间:2017-08-31 20:22:33

标签: xaml listview xamarin data-binding syncfusion

我想插入一个syncfusion linearlayout listview,但由于某种原因,它没有显示任何项目/数据,而且我没有同时收到任何错误,我尝试更改绑定语法和一些东西,但我不能似乎做对了。

这是我的xaml:

            <syncfusion:SfListView x:Name="listView"
                ItemTemplate="{Binding Source={local2:BandInfoRepository}, Path=BandInfo, Mode=TwoWay}"
                ItemSize="100"
                AbsoluteLayout.LayoutBounds="1,1,1,1" 
                AbsoluteLayout.LayoutFlags="All" >
                <syncfusion:SfListView.ItemTemplate>
                    <DataTemplate>
                        <Grid RowSpacing="0" Padding="0,12,8,0" ColumnSpacing="0" Margin="0">
                            <Grid.RowDefinitions>
                                <RowDefinition Height="Auto" />
                                <RowDefinition Height="1" />
                            </Grid.RowDefinitions>
                            <Grid RowSpacing="0" Padding="8,0,8,10">
                                <Grid.RowDefinitions>
                                    <RowDefinition Height="auto" />
                                </Grid.RowDefinitions>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <Image Source="{Binding Path=BandImage}"
                                    Grid.Column="0"
                                    Grid.Row="0"
                                    HeightRequest="80"
                                    WidthRequest="70"
                                    HorizontalOptions="Start"
                                    VerticalOptions="Start"
                                />
                                <StackLayout Orientation="Vertical" 
                                    Padding="5,-5,0,0"
                                    VerticalOptions="Start"
                                    Grid.Row="0"
                                    Grid.Column="1">
                                    <Label Text="{Binding Path=BandName}" 
                                        FontAttributes="Bold"
                                        FontSize="16"
                                        BackgroundColor="Green"
                                        TextColor="#000000" />
                                    <Label Text="{Binding Path=BandDescription}"
                                        Opacity="0.54"
                                        BackgroundColor="Olive"
                                        TextColor="#000000"
                                        FontSize="13" />
                                </StackLayout>
                            </Grid>
                            <BoxView Grid.Row="1" 
                                HeightRequest="1"
                                Opacity="0.75"
                                BackgroundColor="#CECECE" />
                        </Grid>
                    </DataTemplate>
                </syncfusion:SfListView.ItemTemplate>
            </syncfusion:SfListView>

这是我从中获取数据的类:

public class BandInfo : INotifyPropertyChanged
{
    private string bandName;
    private string bandDesc;
    private ImageSource _bandImage;
    public string BandName
    {
        get { return bandName; }
        set
        {
            bandName = value;
            OnPropertyChanged("BandName");
        }
    }

    public string BandDescription
    {
        get { return bandDesc; }
        set
        {
            bandDesc = value;
            OnPropertyChanged("BandDescription");
        }
    }

    public ImageSource BandImage
    {
        get { return _bandImage; }
        set
        {
            _bandImage = value;
            OnPropertyChanged("BandImage");
        }
    }

    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string name)
    {
        if (this.PropertyChanged != null)
            this.PropertyChanged(this, new PropertyChangedEventArgs(name));
    }
}

以防万一,这就是我填充集合的方式(BandInfoRepository.cs):

public class BandInfoRepository
{
    private ObservableCollection<BandInfo> bandInfo;

    public ObservableCollection<BandInfo> BandInfo
    {
        get { return bandInfo; }
        set { this.bandInfo = value; }
    }

    public BandInfoRepository()
    {
        GenerateBookInfo();
    }

    internal void GenerateBookInfo()
    {
        string[] BandNames = new string[] {
            "Nirvana",
            "Metallica",
            "Frank Sinatra"
        };

        string[] BandDescriptions = new string[] {
            "Description",
            "Description",
            "Description"
        };

        bandInfo = new ObservableCollection<BandInfo>();

        for (int i = 0; i < BandNames.Count(); i++)
        {
            var band = new BandInfo()
            {
                BandName = BandNames[i],
                BandDescription = BandDescriptions[i],
                BandImage = ImageSource.FromResource("Lim.Images.Image" + i + ".png")
            };
            bandInfo.Add(band);
        }
    }
}

我希望你们可以帮助我,因为我已经坚持了一段时间了。提前谢谢。

2 个答案:

答案 0 :(得分:1)

看起来你无意中绑定了ItemTemplate两次并且没有绑定任何绑定 ItemsSource甚至一次。

答案 1 :(得分:0)

我们已经查看了您的代码片段,并且我们发现您已将基础集合绑定到ItemTemplate属性而不是ItemsSource属性。除了绑定底层集合之外,还必须将ViewModel(即BandInfoRepository)设置为ContentPage的BindingContext。请参阅以下代码片段,了解如何为您的页面设置BindingContext,以及将基础集合绑定到ItemsSource属性。

代码示例:[XAML]

<ContentPage>
  <ContentPage.BindingContext>
    <local:BandInfoRepository/>
  </ContentPage.BindingContext>

  <ContentPage.Content>
    <listView:SfListView x:Name="listView" ItemSize="70" ItemsSource="{Binding BandInfo}" >

      <listView:SfListView.ItemTemplate>
        <DataTemplate>
          <ViewCell>
            <ViewCell.View>
              <Grid x:Name="grid" RowSpacing="1">
                <Grid.RowDefinitions>
                  <RowDefinition Height="*" />
                  <RowDefinition Height="1" />
                </Grid.RowDefinitions>
                <Grid RowSpacing="1">
                  <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="50" />
                    <ColumnDefinition Width="*" />
                  </Grid.ColumnDefinitions>
                  <Image Source="{Binding BandImage}"
     VerticalOptions="Center"
     HorizontalOptions="Center"
     HeightRequest="50" Aspect="AspectFit"/>
                  <Grid Grid.Column="1"
    RowSpacing="1"
    Padding="10,0,0,0"
    VerticalOptions="Center">
                    <Label Text="{Binding ContactName}"/>
                  </Grid>
                </Grid>
                <StackLayout Grid.Row="1" BackgroundColor="Gray" HeightRequest="1"/>
              </Grid>
            </ViewCell.View>
          </ViewCell>
        </DataTemplate>
      </listView:SfListView.ItemTemplate>
    </listView:SfListView>
  </ContentPage.Content>
</ContentPage>

为了您的帮助,我们附上了下面的工作示例链接。

示例链接:http://www.syncfusion.com/downloads/support/directtrac/186932/ze/ListViewSample905947849