我试图从SQLiteXamarin Forms文件中的iOS文件中获取所有信息,并将其放在Label的单个cell中的TableView中。我正在尝试以下操作:

public partial class ErrorCell : UITableViewCell
{
    public ErrorCell (IntPtr handle) : base (handle)
    {
    }

   internal void UpdateCell(Error error)
   {
        errorDescription.Text = error.Description;
    }
}

 class TableSource : UITableViewSource
{
    List<Error> errors;

    public TableSource(List<Error> errors)
    {
        this.errors = errors;
    }

    public override UITableViewCell GetCell(UITableView tableView, NSIndexPath indexPath)
    {
        var cell = (ErrorCell)tableView.DequeueReusableCell("cell_Id", indexPath);
        var errorid = errors[indexPath.Row];
        cell.UpdateCell(errorid);
        return cell;
    }
    public override nint RowsInSection(UITableView tableview, nint section)
    {
        return errors.Count;
    }
}


当我尝试从获取所有项目的数据库中调用方法时

 public class ErrorDataBase
{
    readonly SQLiteAsyncConnection database;

    public ErrorDataBase(string dbPath)
    {
        database = new SQLiteAsyncConnection(dbPath);
        database.CreateTableAsync<Error>().Wait();
    }

    public Task<List<Error>> GetItemsAsync()
    {
        return database.Table<Error>().ToListAsync();
    }
}


在这里:

 public partial class ViewController : UIViewController
{
    static ErrorDataBase database;
    public ViewController(IntPtr handle) : base(handle)
    {
    }
    List<Error> errors;
    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        errors = new List<Error>
        {
            new Error()
            {
                Description="hufhdsuhufds"
            }
            ,
            new Error()
            {

                Description="Robot died and now we have to go and buy another robot"
            }
            ,
            new Error()
            {
                Description="Dead Robot revived!"
            }
        };

        errorListView.Source = new TableSource(errors);
        //errorListView.Source = await Database.GetItemsAsync();
        errorListView.RowHeight = UITableView.AutomaticDimension;
        errorListView.EstimatedRowHeight = 90f;
        errorListView.ReloadData();
    }
    public static ErrorDataBase Database
    {
        get
        {
            if (database == null)
            {
                database = new ErrorDataBase(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "SQLiteDB.db"));
            }
            return database;
        }
    }
}




 errorListView.Source = await Database.GetItemsAsync();


我收到以下错误:


  无法从SystemCollectionsGeneric.List转换为
  UITableViewSource


我的开发经验真的无法解决这个问题,有人可以帮我吗?

最佳答案

您正在将List分配给UITableViewSource,类型不匹配。

修改为

var list =  await Database.GetItemsAsync();
errorListView.Source = new TableSource(list);

09-07 14:24