使用 DataTemplate 时,ListView 仅显示列表中的最后一项

ListView show only the last item in a list when using DataTemplate

我正在将客户列表绑定到 Xamarin Listview。我想使用带有 ViewCell 的 DataTemplate。

根据文档,我们可以使用 2 个构造函数之一:

  1. new DataTemplate(typeof(CustomerViewCell));

  2. new DataTemplate(=>new CustomerViewCell);

然而,它们导致不同的结果。第一个正确显示列表,而第二个显示重复列表的最后一项。

我对这里的基本知识一无所知吗?

这是我的客户模型:

 public class Customers : List<Customer>
    {

        public Customers()
        {
            Add(new Customer { No = 1, Name = "Microsoft" });
            Add(new Customer { No = 2, Name = "Google" });
            Add(new Customer { No = 3, Name = "Facebook" });
        }

    }

    public class Customer {

       public int No { get; set; }
        public string Name { get; set; }

    }

这里是继承自ViewCell

的CustomerViewCell.xaml
<?xml version="1.0" encoding="UTF-8"?>
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
             xmlns:d="http://xamarin.com/schemas/2014/forms/design"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             mc:Ignorable="d"
             x:Class="LVDataTemplate.View.CustomerViewCell">
  <ViewCell.View>
      <StackLayout Orientation="Horizontal">
          <Label Text="{Binding No}" />
          <Label Text="{Binding Name}" />
        </StackLayout>
  </ViewCell.View>
</ViewCell>

CustomerListView,显示客户:


    public partial class CustomerListView :ContentPage
    {

        public CustomerListView()
        {
            InitializeComponent();

            MyListView.ItemsSource = new Customers();

            //1. Work correctly 
            // MyListView.ItemTemplate = new DataTemplate(typeof(CustomerViewCell));

            //2. Work in-correctly. only the last item repeated through out the list
            var theCell = new CustomerViewCell();
            MyListView.ItemTemplate = new DataTemplate(()=>theCell);

        }


    }

第一个代码显示结果:

  1. 微软。
  2. Google
  3. 脸书

第二场演出

  1. 脸书
  2. 脸书
  3. 脸书

请在此处阅读文档:data-templates/creating

方法public DataTemplate(Func<object> loadTemplate);用于创建数据模板作为资源,因此第二个构造函数的正确用法是:

var personDataTemplate = new DataTemplate(() => {

        return new ViewCell1();
    });


    Resources = new ResourceDictionary();
    Resources.Add("personTemplate", personDataTemplate);

    testListView.ItemTemplate = (DataTemplate)Resources["personTemplate"];

或者您可以像这样自定义 ViewCell:

var personDataTemplate = new DataTemplate (() => {
  var grid = new Grid ();
  ...
  return new ViewCell { View = grid };
});

此外,您可以直接使用 personDataTemplate 而无需将其定义为资源:

 testListView.ItemTemplate = personDataTemplate;

你的代码的问题是你应该在块内创建 theCell,你可以使用:

    var personDataTemplate = new DataTemplate(() => {

        return new ViewCell1();
    });

public partial class MainPage : ContentPage
{
    public MainPage()
    {
        InitializeComponent();       
        testListView.ItemTemplate = new DataTemplate(() => test());
    }

    public ViewCell1 test() {

        //other actions
        return new ViewCell1();
    }
}

第一个构造函数MyListView.ItemTemplate = new DataTemplate(typeof(CustomerViewCell));creating-a-datatemplate-with-a-type,正确