【问题标题】:Persistent Storage using Application.Current.Properties not working使用 Application.Current.Properties 的持久存储不起作用
【发布时间】:2021-08-02 15:57:22
【问题描述】:

我正在尝试在 Xamarin.Forms 中实现持久存储。在 Xamarin.Forms 中研究后,我决定使用Application.Current.Properties 属性。

看起来只有当应用程序仍然存在时它才能工作。如果我关闭应用程序并重新启动它,Application.Current.Properties 是空的。

有谁知道我做错了什么?我可以通过其他方式实现此功能吗?

像往常一样,谢谢大家。

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    我在 Android 上使用 Application.Current.Properties 时遇到了很多问题。我强烈建议使用Xamarin Settings 插件,我从来没有遇到过任何问题。即使应用程序关闭,它也是持久的。

    话虽如此,Application.Current.Properties 应该可以在您关闭应用程序时工作。不知道为什么它不会,但它也不会让我感到惊讶。

    *编辑:要在安装后使用,基本上CrossSettings.Current 是可以完成工作的插件类,但该示例只是创建了一个单独的属性来访问它。所以创建一个新文件,我们称之为SettingsImplementation

    public static class SettingsImplementation {
    
        #region Instance
    
        private static Lazy<ISettings> _appSettings;
    
        public static ISettings AppSettings {
            get {
                if(_appSettings == null) {
                    _appSettings = new Lazy<ISettings>(() => CrossSettings.Current, LazyThreadSafetyMode.PublicationOnly);
                }
    
                return _appSettings.Value;
            }
            set {
                _appSettings = new Lazy<ISettings>(() => value, LazyThreadSafetyMode.PublicationOnly);
            }
        }
    
        #endregion
    
        private const string UserNameKey = "username_key"; //Key used to get your property
        private static readonly string UserNameDefault = string.Empty; //Default value for your property if the key-value pair has not been created yet
    
        public static string UserName {
            get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
            set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
        }
    }
    

    然后要使用它,您可以在应用程序的任何地方执行此操作:

    SettingsImplementation.UserName = "something";
    

    string username = SettingsImplementation.UserName;
    

    【讨论】:

    • 嗨@hvaughan3,首先感谢您的回答。看起来对我有用。一个愚蠢的问题,我不能使用手册后面的插件......你能帮我吗?
    • 我安装了软件包,然后将其添加到每个解决方案中,但我不知道我做错了什么。我不能使用 AppSettings.GetValueOrDefault...
    • 公共静态类设置是在我的 rootnamespace.ViewModel.Helpers 中创建的,但 Visual 无法识别该类...
    • @EliasMP 抱歉,刚刚看到这个。让我编辑我的答案。
    • 感谢伙伴帮助我。 :)
    【解决方案2】:

    我自己关于这个问题的问题是由于我没有使用以下代码行明确保存属性:

    Application.Current.SavePropertiesAsync();
    

    【讨论】:

    • 您的代码应该在哪里以及如何使用?
    【解决方案3】:

    您可以改用 Xamarin 基本要素“首选项”:

    Preferences.Set("Key", "Value");
    Preferences.Get("Key", "Default");
    

    【讨论】:

      【解决方案4】:

      我遇到了同样的问题。

      问题:

      我试图将复杂对象放入应用程序属性中。

      事实证明,Properties 只能接受原始数据类型。

      这个博客很有帮助。

      https://codemilltech.com/persist-whatever-you-want-with-xamarin-forms/

      【讨论】:

      • 建议包含来自提供解决方案的链接的一些信息。这里的要点是:要解决您只能将原始类型(例如字符串)保存在Application.Current.Properties 中的事实,您可以将对象序列化为JSON 字符串并像这样保存:)
      猜你喜欢
      • 2016-08-07
      • 1970-01-01
      • 2016-05-16
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-15
      • 1970-01-01
      相关资源
      最近更新 更多