You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Xamarin中无法绑定到System.String问题求助

解决Xamarin ListView绑定Game对象时的绑定转换错误

咱们来一步步搞定这个绑定报错的问题:

报错原因分析

当你把db.Table<Game>().OrderBy(x => x.GameNaam).ToList()直接赋值给ListView.ItemsSource时,ListView默认会用TextCell作为列表项模板。而TextCell默认会尝试绑定到数据对象的Text属性,但你的Game类并没有这个属性,绑定系统就会试图把整个Game实例转换成字符串,这就触发了你看到的"..." can not be converted to type 'System.String'错误。

解决方案1:利用现有ToString方法绑定

既然你的Game类已经重写了ToString()方法,我们可以让TextCell直接绑定到整个Game对象(用.表示当前绑定上下文),这样就能显示你自定义的字符串了。修改视图代码如下:

public GetAllGamesPage()
{
    this.Title = "WishList";
    var db = new SQLiteConnection(_dbPath);
    StackLayout stackLayout = new StackLayout();
    
    _listView = new ListView();
    // 设置ItemTemplate为TextCell,并绑定到当前对象
    _listView.ItemTemplate = new DataTemplate(() =>
    {
        var textCell = new TextCell();
        textCell.SetBinding(TextCell.TextProperty, ".");
        return textCell;
    });
    
    _listView.ItemsSource = db.Table<Game>().OrderBy(x => x.GameNaam).ToList();
    stackLayout.Children.Add(_listView);
    Content = stackLayout;
}

解决方案2:自定义ViewCell展示三个属性

如果你想分别展示GameNaam、GameGenre、GameRelease这三个属性,可以创建一个自定义的ViewCell,用StackLayout来布局每个属性的标签:

public GetAllGamesPage()
{
    this.Title = "WishList";
    var db = new SQLiteConnection(_dbPath);
    StackLayout stackLayout = new StackLayout();
    
    _listView = new ListView();
    _listView.ItemTemplate = new DataTemplate(() =>
    {
        // 创建三个标签分别显示不同属性
        var nameLabel = new Label { FontAttributes = FontAttributes.Bold };
        nameLabel.SetBinding(Label.TextProperty, "GameNaam");
        
        var genreLabel = new Label { TextColor = Color.Gray };
        genreLabel.SetBinding(Label.TextProperty, "GameGenre");
        
        var releaseLabel = new Label { TextColor = Color.DarkGray };
        releaseLabel.SetBinding(Label.TextProperty, "GameRelease");
        
        // 用垂直StackLayout组合标签
        var cellLayout = new StackLayout { Padding = new Thickness(10) };
        cellLayout.Children.Add(nameLabel);
        cellLayout.Children.Add(genreLabel);
        cellLayout.Children.Add(releaseLabel);
        
        return new ViewCell { View = cellLayout };
    });
    
    _listView.ItemsSource = db.Table<Game>().OrderBy(x => x.GameNaam).ToList();
    stackLayout.Children.Add(_listView);
    Content = stackLayout;
}

额外小建议

记得把你的Game类改成public的,当前它是默认的内部类,跨程序集绑定可能会有潜在问题:

namespace Test1.Models
{
    // 加上public修饰符
    public class Game 
    {
        [PrimaryKey]
        public int Id { get; set; }
        public string GameNaam { get; set; }
        public string GameGenre { get; set; }
        public string GameRelease { get; set; }

        public override string ToString()
        {
            return this.GameNaam + " " + this.GameGenre + " " + this.GameRelease;
        }
    }
}

内容的提问来源于stack exchange,提问作者Felix

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:52:38