如何解决从 SQL Lite DB 动态创建 ListView 行
我希望创建一个应用程序,根据 sql 数据库条目自动创建一个 ListView 行 - 最多 5 个 ListView 行。
人物类:
using sqlite;
namespace AppPeople
{
public class Person
{
[PrimaryKey,AutoIncrement]
public int ID { get; set; }
public string Name { get; set; }
public int Age { get; set; }
}
}
数据库.cs:
using System.Collections.Generic;
using System.Threading.Tasks;
using sqlite;
namespace AppPeople
{
public class Database
{
readonly sqliteAsyncConnection _database;
public Database(string dbPath)
{
_database = new sqliteAsyncConnection(dbPath);
_database.CreateTableAsync<Person>().Wait();
}
public Task<List<Person>> GetPeopleAsync()
{
return _database.Table<Person>().ToListAsync();
}
public Task<int> SavePersonAsync(Person person)
{
return _database.InsertAsync(person);
}
}
}
MainPage.xaml:
<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="AppPeople.MainPage">
<ListView x:Name="saved_properties_list">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Horizontal"
HorizontalOptions="FillAndExpand"
Margin="20,10,20,0" >
<Label Text="{Binding Name}" HorizontalOptions="StartAndExpand" />
<Label Text="{Binding Age}" />
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
MainPage.cs:
protected override async void OnAppearing()
{
base.OnAppearing();
saved_properties_list.ItemsSource = await MainPage.Database.GetPropertiesAsync();
}
public Task> GetPropertiesAsync()
{
return database.Table().ToListAsync();
}
我将如何更改此设置,以便根据最多包含 5 个 ListView 行的 sql 数据库条目自动创建 ListView 行?谢谢。
解决方法
使用 LINQ Take
限制结果数量
return _database.Table<Person>().Take(5).ToListAsync();
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。