如何根据 SQLite 数据库 table 中的 table 创建选择器显示选项?

How can I make a Picker display options based on a table in a SQLite DB table?

我使用以下方法从 api 获取值并将它们存储在 table 中。我想检索项目并将它们用作选择器中的选项。 class、方法和选择器的代码实现如下。我可以为 SelectedIndexChanged 编写什么方法来显示项目?

//class
public class Items
    {
        [PrimaryKey, AutoIncrement]
        public int ID { get; set; }
        public string ItemsVariety { get; set; }
    }
}

//method retrieving items and storing them in a SQLite DB
private async Task GetItems()
        {
            var address = App.Server + "GetItems";
            HttpClient client = new HttpClient();
            var response = await client.GetStringAsync(address);
            List<Items> varieties = JsonConvert.DeserializeObject<List<Items>>(response);
            List<Items> varieties1 = new List<Items>();
            Items item = new Items();
             
            foreach (var i in varieties)
            {
                item.ItemsVariety = item.ItemsVariety;
                varieties1.Add(i);
                await App.dBModel.SaveItems(i);
            }
        }

//Code for Picker

 <Picker x:Name="MyItems" SelectedIndexChanged="MyItemsChanged"/>

您的选择器缺少 ItemsSource 绑定。请参阅 documentation here 以了解更多详细信息。

以防万一,如果您不仅将字符串集合绑定到选取器,而且将对象集合绑定到选取器,选取器将默认仅显示对象名称。要在对象中显示 属性 的值,请使用 ItemDisplayBinding property of the Xamarin.Forms.Picker.

Refer here 了解更多详情和示例代码。

希望对您有所帮助。

先从SQLite数据库中获取列表。 您似乎将 json 值保存到 SQLite 数据库中。如果您从数据库接收数据。你可以试试下面的代码。您可以通过从下面的 link 下载源文件来获得更多详细信息。 https://docs.microsoft.com/en-us/samples/xamarin/xamarin-forms-samples/getstarted-notes-database/

_database.Table<Items>().ToListAsync();  //If you use the dBModel as you database, replace the _database with your own database.

绑定sqlite数据库列表到ItemsSource.

 MyItems.ItemsSource = _database.Table<Items>().ToListAsync(); //

然后在数据库模型Picker中设置要显示的ItemDisplayBinding

<Picker
        x:Name="MyItems"
        ItemDisplayBinding="{Binding ItemsVariety}"
        SelectedIndexChanged="MyItemsChanged" />