2017-08-31 132 views
1

我想插入一个syncfusion linearlayout listview,但由于某种原因它不显示任何项目/数据,并且我没有在同一时间收到任何错误,我尝试更改绑定语法和一些事情,但我似乎无法做到。Syncfusion Xamarin 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); 
     } 
    } 
} 

我希望你们能帮助我,因为我一直坚持这一段时间。提前致谢。

+0

为什么ItemTemplate绑定到回购? – Jason

+0

@Jason这是一个班级,我只是举了一个例子,并将班级重命名为我的项目的更多“合适”名称。 –

+0

我将添加整个.cs,以便您可以看到它。 –

回答

1

看起来像是无意中绑定了两次,并且不会绑定任何一次 ItemsSource

+0

让我检查,我是OOP和Xamarin的新手,也会尝试修复它 –

+0

似乎它现在部分工作,但BandImage绑定仍然不起作用,还有一些其他属性,任何想法? –

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