Xamarin Forms ListView ItemsSource 问题

Xamarin Forms ListView ItemsSource issue

我有一个 ListView 及其 ItemsSource。

ListView IList = new ListView();
IList.ItemsSource = _routine_names; 

为了自定义我正在做的每个项目的数据模板:

IList.ItemTemplate = new DataTemplate(()=>
        {
            Label Routine_Name = new Label(); // should be_routine_names
            return new ViewCell
            {
                View = new StackLayout{
                    Children = {Routine_Name}
                }
            };
        });

当我 运行 显示我的 ListView 时,列表项确实存在(它们有可用的 onclick 处理程序)但没有文本,这应该是 _routine_names 中的任何内容。

我的问题是如何让 DataTemplate 中的标签成为 _routine_names 中的项目?

我添加 DataTemplate 的原因是我可以添加滑动删除。

您可以只使用内置的 TextCell 来完成您想要做的事情。它有一个可绑定的 TextProperty 和一个可选的 DetailProperty 如果你想在主文本行下面有一个较小的文本行。

IList.ItempTemplate = new DataTemplate(()=> 
{
    var textCell = new TextCell();
    textCell.ContextActions.Add(yourAction);
    textCell.SetBinding(TextCell.TextProperty, "."); 
    return textCell; 
}

IList.ItemsSource = _routine_names; 

yourAction 属于 MenuItem 类型 here

此外,请注意 IList 是 System.Collection 命名空间中 class 的名称,因此我会在那里使用其他名称。