2017-08-31 172 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