Xamarin Essentials保存对象详细信息

时间:2020-09-15 18:58:33

标签: .net xamarin mobile xamarin.essentials

我正在寻找一种将详细信息列表保存到Xamarin.Essentials.Preferences中的好方法。

基本上类似

<Item>
    <Name>Toast</Name>
    <Weight>0.5oz</Weight>
    <Quantity>4</Quantity>
</Item>

我考虑过进行xml序列化,但是我不确定这是最好的方法。

此列表将少于500个项目,每个项目最多具有6个属性,即名称,重量,数量,ImageRefPath等。

有人有什么建议和/或示例来说明这种存储方式吗?

Items集合中不存在的东西是:

无二进制数据。 没有图像。 没有斑点。

1 个答案:

答案 0 :(得分:0)

也许您可以尝试将项目转换为json字符串,然后将其保存到Xamarin.Essentials.Preferences。

例如,首先定义一个模型:

public class MyItem
{
    public string Name { get; set; }
    public string Weight{ get; set; }
    public string Quantity{get;set;}
    public string ImageRefPath{get;set;}
    ...
}

//convert the items data to json string and save
ObservableCollection<MyItem> ItemList;
string json = JsonConvert.SerializeObject(ItemList);
Xamarin.Essentials.Preferences.Set("list", json);

//get the json string then convert to the List<MyItem>.
string json = Xamarin.Essentials.Preferences.Get("list","");
ObservableCollection<MyItem> items = JsonConvert.DeserializeObject<ObservableCollection<MyItem>>(json );
相关问题