微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Xamarin 表单中保留 AppsContext.Instance 中的数据

如何解决在 Xamarin 表单中保留 AppsContext.Instance 中的数据

  using System;

  namespace KfcBoh.Inventory.MobileUi.Models.User
  {
   public sealed class AppsContext
    {
    #region Properties
    private static AppsContext _instance = null;
    private static readonly object SyncLock = new object();
    public List<StorageData> StorageItemsForPreCount { get; set; } = new List<StorageData>();

    #endregion
    public AppsContext()
    {
    }

    public static AppsContext Instance
    {
        get
        {
            if (_instance == null)
            {
                lock (SyncLock)
                {
                    if (_instance == null)
                    {
                        _instance = new AppsContext();
                    }
                }
            }
            return _instance;
        }
    }
}
}

AppsContext.Instance.StorageItemsForPreCount 在应用程序被杀死时被清除。

当应用在 Xamarin Forms 中被杀死时,如何防止这些数据从列表中删除

解决方法

因为这是一个自定义的类,即使是单例,它也不做任何在设备上存储数据的事情,当应用程序被杀死时它会被销毁。

您可以将您的 StorageItemsForPreCount 转换为 json 字符串,然后使用 Xamarin.Essentials: PreferencesXamarin.Essentials: Secure Storage 来存储它。您只需要将 json 字符串转换为您的 List<StorageData> 再次使用时。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。