【问题标题】:Xamarin.Forms Display Text in ListView instead of typenameXamarin.Forms 在 ListView 中显示文本而不是 typename
【发布时间】:2017-11-24 01:03:22
【问题描述】:

我正在用项目填充 Xamarin.Forms.ListView。

public class LCIDEditor : ContentPage
{
    private ListView _lv;
    private ObservableCollection<TextCell> _ItemList = new ObservableCollection<TextCell>();

    public LCIDEditor()
    {
        this.Title = "LCIDEditor";

        _ItemList.Add(new TextCell { Text = "Englisch", Detail = "1033" });
        _ItemList.Add(new TextCell { Text = "Deutsch", Detail = "1031" });

        _lv = new ListView();
        _lv.ItemsSource = _ItemList;
        _lv.ItemTemplate = new DataTemplate(typeof(TextCell));

        this.Content = _lv;
    }
}

在最好的情况下,我让列表视图在 ListView 中显示类似“Xamarin.Forms.TextCell”的内容,而不是任何真实的文本。

如果我包括

_lv.ItemTemplate = new DataTemplate(typeof(TextCell));

,我什么都看不到,列表视图保持空白。

有人可以告诉我我在这里做错了什么吗? 我通读了文档,但它们没有帮助,而且我看到其他人问同样的问题,但在其他情况下。

我正在尝试处理这个(显然不是这样)简单的例子。

【问题讨论】:

  • 从您的项目创建中删除TextCell。只需 _ItemList.Add(new { Text = "Englisch", Detail = "1033" }); 并将其插入到 ItemTemplate set 命令行之后。
  • @DiegoRafaelSouza 谢谢,但由于各种原因,它不能以这种方式工作。您介意发布整个代码吗?
  • 你是对的,对不起。这是因为ObservableCollection&lt;TextCell&gt;。它必须是ObservableCollection&lt;object&gt;。我将发布整个代码

标签: listview xamarin xamarin.forms


【解决方案1】:

试试这个方法:

public class LCIDEditor : ContentPage
{
    private ListView _lv;
    private ObservableCollection<object> _ItemList = new ObservableCollection<object>();

    public LCIDEditor()
    {
        this.Title = "LCIDEditor";

        _ItemList.Add(new { Text = "Englisch", Detail = "1033" });
        _ItemList.Add(new { Text = "Deutsch", Detail = "1031" });

        _lv = new ListView() 
        {
            ItemTemplate = new DataTemplate(typeof(TextCell)), 
            ItemsSource = _ItemList
        };

        this.Content = _lv;
    }
}
  • TextCell 必须像模板一样使用,而不是像项一样使用;
  • 当 DataTemplate 的 ViewCell 未绑定或对象的类型未被识别为有效项时,它会显示 ToList() 结果 - 默认为类型名称。

你应该测试一下代码,我这里没有运行。

希望对你有用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-28
    • 2020-08-27
    • 1970-01-01
    • 2022-01-17
    相关资源
    最近更新 更多