简体   繁体   中英

Xamarin: Populating ListView in xcml using a List<T>

I'm having issues getting the data binding to work in xaml with ListView. The ListView displays all cells as expected but doesn't display the value for any of the bound variables.

I have my sample data constructed like this:

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);
    }
}

The ListView in xaml is defined this way:

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

I have created 200 sample objects and assigned to uiList.ItemsSource. This results in the ListView showing 200 items as expected but it doesn't show the value of the Message property in the DataItem.

I've also tried using {Binding Message} (without Path=) but the result didn't change. I'm sure there is something obvious I'm missing but can't figure it out.

--- UPDATE ---

I have a reliable repro now. Create a new project and add the ListView and population as provided above.

Go to the solution properties for the iOS project, select the iOS Build page and change 'Link behavior' to 'Link All' and change the 'Supported architecture' to 'x86_64'. Now Clean, Build and Run and the binding will not work.

Hey I just tried your code and it worked for me out of the box. I just added my logic to populate a list of DataItem and set it as ItemsSource of the listview. Here I is my code and the result.

<?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输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM