Xamarin:使用List <t>

时间:2016-04-21 04:58:49

标签: c# listview xamarin

我在使用ListView在xaml中使用数据绑定时遇到问题。 ListView按预期显示所有单元格,但不显示任何绑定变量的值。

我的样本数据构造如下:

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属性的值。

我也尝试过使用{绑定消息}(没有Path =),但结果没有改变。我确定有一些显而易见的东西我不知道但是无法弄清楚。

---更新---

我现在有一个可靠的复制品。创建一个新项目并添加上面提供的ListView和填充。

转到iOS项目的解决方案属性,选择iOS Build页面并更改&#39; Link behavior&#39;到“全部链接”#39;并改变“支持的架构”#39;到&#39; x86_64&#39;。现在清理,构建和运行,绑定将不起作用。

1 个答案:

答案 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