2016-04-21 26 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

奇怪的事情正在發生。我創建了一個新項目並複製/粘貼了代碼並按預期工作。我不能在新項目中複製它,但舊項目仍然固執而不起作用。 我試過乾淨所有和重建所有,但結果保持不變。現在挖掘更多。 –