【问题标题】:Can I save the contents of an ObservableCollection so that the list is still intact when I restart my app?我可以保存 ObservableCollection 的内容,以便在我重新启动我的应用程序时列表仍然完整吗?
【发布时间】:2017-05-31 11:13:46
【问题描述】:

我想做的是让用户将项目添加到列表中。然后当他们添加一个项目时,我需要保存列表,这样当用户关闭应用程序并再次打开它时,他们创建的列表仍然存在。

现在,我可以将项目添加到我的列表中,但一旦我关闭应用程序,它们就会消失。

private static ObservableCollection<ViewModels.ZoneViewModel> Zones = new ObservableCollection<ViewModels.ZoneViewModel>();

    public void PopulateListView(string image, string name, string address)
    {
        if (name != "" && address != "")
        {
            Zones.Add(new ViewModels.ZoneViewModel { Image = image, Name = name, Address = address });

            Application.Current.Properties["zoneslist"] = Zones;
        }
    }

    protected override void OnAppearing()
    {
       if (Application.Current.Properties.ContainsKey("zoneslist"))
       {
          // Put the contents of the "zoneslist" key into a variable as a string.
          var savedZones = Application.Current.Properties["zoneslist"] as ObservableCollection<ViewModels.ZoneViewModel>;

           // Set the listviews' itemssource to the savedzones list.
           zonesList.ItemsSource = savedZones;
       }
    }

这是我现在使用的代码,我认为这可以保存它,但它不起作用。

编辑:所以我尝试了@Alessandro Calario 的建议,在使用 json 序列化之后,列表视图只给了我大量的空列表项(即使我只添加了一个)。但是即使应用程序关闭,也会添加并保存一个项目。至少取得了进展,但我还没有完全做到。有人知道解决办法吗?

我的代码:

public void PopulateListView(string image, string name, string address)
{
    if (name != "" && address != "")
    {
        Zones.Add(new ViewModels.ZoneViewModel { Image = image, Name = name, Address = address });

        //Serialize to json string
        var json = JsonConvert.SerializeObject(Zones);

        Application.Current.Properties["zoneslist"] = json;
    }
}

protected override void OnAppearing()
{
    if (Application.Current.Properties.ContainsKey("zoneslist"))
    {
        // Put the contents of the "zoneslist" key into a variable as a string.
        var savedZones = Application.Current.Properties["zoneslist"] as string; //ObservableCollection<ViewModels.ZoneViewModel>

        JsonConvert.DeserializeObject<ObservableCollection<ViewModels.ZoneViewModel>>(savedZones);

        // Set the listviews' itemssource to the savedzones list.
        zonesList.ItemsSource = savedZones;
    }
}

【问题讨论】:

    标签: c# listview xamarin xamarin.forms observablecollection


    【解决方案1】:

    我认为您可以将对象列表序列化为 json 字符串并将其保存到应用程序属性

    【讨论】:

    • 是的,我正在考虑这样做。我现在就试试。
    • 好吧,我试过了,它保存了,但是添加一个项目后,我的列表视图中现在有大量空列表项。我将添加我在问题中使用的代码,您可以看看吗?
    • savedZones 是一个字符串。您应该反序列化为一个对象... Account account = JsonConvert.DeserializeObject(json);你反序列化什么都不做。并将对象传递给您的 ItemsSource(您正在传递字符串...)
    【解决方案2】:

    如果您的项目不适合使用 3rd 方库,我强烈建议您使用 Akavache。这是一个异步的、持久的键值对存储。

    设置后使用起来非常简单。

    //插入你的对象

    IObservable<Unit> InsertObject<T>(string key, T value, DateTimeOffset? absoluteExpiration = null);
    

    //获取你的对象

    IObservable<T> GetObject<T>(string key);
    

    T 可以是您的整个列表。

    当然不止于此,但请相信我一点。阅读完整的文档,希望它符合您的需求。

    【讨论】:

    • 这对我来说似乎是一个完美的选择,但我已经尝试过了,我的应用程序保存了列表,但是如果我添加项目,关闭应用程序,重新打开它并添加另一个项目覆盖当前列表并添加单个项目。我认为这是因为每次启动我的应用程序时它仍然会创建一个新的列表实例。所以旧的仍然设置为应用程序启动时列表视图的项目源。有没有办法解决这个问题?
    • 您可以读取缓存的内容,如果有任何可用的内容,您可以修改刚刚读取的列表并保存回来。
    • 所以你的意思是像var _zonesList = await BlobCache.LocalMachine.GetObject&lt;ObservableCollection&lt;ViewModels.ZoneViewModel&gt;&gt;("zones"); 然后将项目添加到 zoneList 并再次保存?因为那行不通。当我尝试向 zoneList 添加任何内容时,我得到一个 NullReferenceException。我不确定为什么会这样。
    【解决方案3】:

    应用程序属性仅存储原始类型。

    注意:Properties 字典只能序列化原始类型 贮存。尝试存储其他类型(例如 List 可以 默默地失败)。 来源:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/application-class/

    要么设置它,以便将属性用作原始存储,要么使用另一种本地存储机制,例如 Sqlite(这里有一个很好的指南:https://developer.xamarin.com/guides/xamarin-forms/application-fundamentals/databases/

    【讨论】:

    • 我尝试使用属性作为原始存储进行设置,但遇到了另一个问题。你能看看我的编辑吗?我在问题中添加了我现在所拥有的内容。
    猜你喜欢
    • 1970-01-01
    • 2013-01-09
    • 2020-08-07
    • 1970-01-01
    • 2011-11-10
    • 1970-01-01
    • 2022-09-23
    • 2015-07-12
    • 1970-01-01
    相关资源
    最近更新 更多