gpt4 book ai didi

c# - 我想从 SQLite DB 获取所有信息

转载 作者:行者123 更新时间:2023-11-29 05:31:21 25 4
gpt4 key购买 nike

我正在尝试从 Xamarin Forms for iOS 中的 SQLite 文件获取所有信息,并将其放入 LabelTableView 中的单个 cell 中。我正在尝试以下操作:

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();

我收到以下错误:

Cannot convert from SystemCollectionsGeneric.List to UITableViewSource

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

最佳答案

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

修改为

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

关于c# - 我想从 SQLite DB 获取所有信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57521971/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com