2016-04-21 45 views
0

我在使用ListView在xaml中获取数据绑定的问题。 ListView按预期方式显示所有单元格,但不显示任何绑定变量的值。Xamarin:使用列表在xcml中填充ListView <T>

我有样本数据构造是这样的:

public class DataItem 
{ 
    public int Number { get; set; } 

    public string Message { get; set; } 

    public DataItem (int i) 
    { 
     this.Number = i + 1; 
     this.Message = string.Format ("Data Item Hello {0}", i + 1); 
    } 
} 

在XAML中的ListView定义是这样的:

 <ListView x:Name="uiList"> 
      <ListView.ItemTemplate> 
       <DataTemplate> 
        <ViewCell> 
         <StackLayout Orientation="Vertical" BackgroundColor="Silver"> 
          <Label TextColor="Green" Text="Pre Xamarin" /> 
          <Label BackgroundColor="Aqua" TextColor="Red" Text="{Binding Path=Message}" /> 
          <Label TextColor="Green" Text="Hello Xamarin" /> 
         </StackLayout> 
        </ViewCell> 
       </DataTemplate> 
      </ListView.ItemTemplate> 
     </ListView> 

我已创建200个样本对象和分配给uiList.ItemsSource。这会导致ListView按预期显示200个项目,但它不显示DataItem中的Message属性的值。

我也尝试使用{绑定消息}(没有路径=),但结果没有改变。我确定有一些明显的我失踪,但无法弄清楚。

---更新---

我现在有一个可靠的repro。创建一个新项目并添加上面提供的ListView和人口。

转到iOS项目的解决方案属性,选择iOS Build页面并将“链接行为”更改为“全部链接”,并将“支持的体系结构”更改为“x86_64”。现在清洁,构建和运行,绑定将不起作用。

+0

也许有你的模拟器/设备旧引用。清理并重建您的项目,因为您的代码似乎有效(根据一个答案)。 –

回答

1

嘿,我只是试过你的代码,它开箱即用。我只是添加了我的逻辑来填充DataItem的列表并将其设置为列表视图的ItemsSource。在这里,我是我的代码和结果。

<?xml version="1.0" encoding="UTF-8"?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestForms.TestListView" xmlns:ffimageloading="clr-namespace:FFImageLoading.Forms;assembly=FFImageLoading.Forms"> 
<ContentPage.Content> 
<ListView x:Name="uiList"> 
     <ListView.ItemTemplate> 
      <DataTemplate> 
       <ViewCell> 
        <StackLayout Orientation="Vertical" BackgroundColor="Silver"> 
         <Label TextColor="Green" Text="Pre Xamarin" /> 
         <Label BackgroundColor="Aqua" TextColor="Red" Text="{Binding Message}" /> 
         <Label TextColor="Green" Text="Hello Xamarin" /> 
        </StackLayout> 
       </ViewCell> 
      </DataTemplate> 
     </ListView.ItemTemplate> 
    </ListView> 
</ContentPage.Content> 

public partial class TestListView : ContentPage 
{ 

    public TestListView() 
    { 
     InitializeComponent(); 

     ObservableCollection<DataItem> employeeList = new ObservableCollection<DataItem>(); 
     for(int i=0;i<100;i++) 
      employeeList.Add(new DataItem(i)); 
     uiList.ItemsSource = employeeList; 

     //Mr. Mono will be added to the ListView because it uses an ObservableCollection 


    } 
} 
public class DataItem 
{ 
    public int Number { get; set; } 

    public string Message { get; set; } 

    public DataItem (int i) 
    { 
     this.Number = i + 1; 
     this.Message = string.Format ("Data Item Hello {0}", i + 1); 
    } 
} 

iOS Output

+0

奇怪的事情正在发生。我创建了一个新项目并复制/粘贴了代码并按预期工作。我不能在新项目中复制它,但旧项目仍然固执而不起作用。 我试过干净所有和重建所有,但结果保持不变。现在挖掘更多。 –