【问题标题】:How to store App settings in Xamarin.Forms如何在 Xamarin.Forms 中存储应用程序设置
【发布时间】:2016-09-13 00:04:33
【问题描述】:

我们如何在 Xamarin.Forms 中将应用设置存储和检索为键值对? 就像当应用程序关闭时,我们可以存储用户偏好,并且在重新启动应用程序时,我们可以获得这些值。

【问题讨论】:

标签: xamarin xamarin.forms


【解决方案1】:

使用Properties Dictionary也可以这样做

用于存储数据:

Application.Current.Properties ["id"] = someClass.ID;

用于获取数据:

if (Application.Current.Properties.ContainsKey("id"))
{
    var id = Application.Current.Properties ["id"] as int;
    // do something with id
}

参考:https://developer.xamarin.com/guides/xamarin-forms/working-with/application-class/#Properties_Dictionary

【讨论】:

  • 请注意,您不应将其用于用户设置。这些属性被序列化为一个 xml 文件,并且不使用底层平台的本机设置/首选项机制。例如。在 iOS 中,它不会在 NSUserDefaults 中存储项目。您将无法从系统的设置包中修改它 (developer.apple.com/library/content/documentation/Cocoa/…)
  • Application.Current.Properties 更多的是关于在激活期间保留应用程序状态 (OnStart/OnSleep/OnResume)
【解决方案2】:

Application 对象(在 VS 中你会得到一个继承自它的 App 类,我认为 Xamarin Studio 模板可能略有不同)有一个专门用于此的 Properties 字典。如果您需要确保立即保存您的属性,可以调用 Application.SavePropertiesAsync 方法。

【讨论】:

  • 对于任何想知道为什么他们的代码不保留您设置的属性的人,您需要使用上面的方法保存它们!我在这个上面拔了一点头发。谢谢!
【解决方案3】:

我将Settings Plugin for Xamarin And Windows 用于Xamarin.Forms,因为这是在设备级别实现的,因此您可以从任何Forms 项目、PCL 库或在您的本地项目中访问这些设置应用程序。

  • Nuget:Xam.Plugins.Settings

private const string UserNameKey = "username_key";
private static readonly string UserNameDefault = string.Empty;

public static string UserName
{
  get { return AppSettings.GetValueOrDefault<string>(UserNameKey, UserNameDefault); }
  set { AppSettings.AddOrUpdateValue<string>(UserNameKey, value); }
}

【讨论】:

  • akavache 怎么样,你用过吗?@SushiHangover
  • @AkashAmin 我目前几乎所有的应用程序都包含Akavache(将其用于缓存,但希望迁移到 Realm 以提高速度并为未来的项目消除 SQLite 依赖)。但是对于存储一些应用程序设置,它有点过头了......
  • 太棒了。我也在研究领域。不过感谢您的反馈。
  • @AkashAmin 没问题,我对 Xamarin 的 Realm 非常感兴趣,因为我曾经在基于 iOS Swift 的项目中使用过它,并且印象非常深刻,Xamarin 综合基准测试看起来非常有前途,而且我们知道速度在移动设备中是王道(!)...但是像任何事情一样,我需要对我正在缓存的数据进行一些测试,以确定它是否真的更适合...
  • 是的,期待它。谢谢。@SushiHangover
【解决方案4】:

您可以使用 Xamarin.Essential 中包含的首选项。

        /* You need to using Xamarin.Essentials
        If you do not have Xamarin.Essentials installed,
        install it from (NuGet Package Manager)
        */

        // Add a reference to Xamarin.Essentials in your class
        using Xamarin.Essentials;

        // To save a value for a given key in preferences
        Preferences.Set("my_key", "my_value");

        // To retrieve a value from preferences or a default if not set
        var myValue = Preferences.Get("my_key", "default_value");

        // To check if a given key exists in preferences
        bool hasKey = Preferences.ContainsKey("my_key");

        // To remove the key from preferences
        Preferences.Remove("my_key");

        // To remove all preferences
        Preferences.Clear();

看看这个链接: https://docs.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android

【讨论】:

    【解决方案5】:

    如果设置更多的是在运行时未更改的配置(例如,应用程序的 web.config 排序),您可以使用 App.xaml 并将值存储在字典中,如下所示:

    <Application.Resources>
        <ResourceDictionary>
            <Style x:Key="SmallLabel" TargetType="Label">
                <Setter Property="TextColor" Value="Gray" />
                <Setter Property="Font" Value="{StaticResource FontItalicSmall}" />
            </Style>
        </ResourceDictionary>
    </Application.Resources>
    

    然后像这样在 C# 代码中检索它们:

    var style = (Style)Application.Current.Resources["SmallLabel"];
    

    【讨论】:

      猜你喜欢
      • 2011-07-05
      • 2021-07-26
      • 1970-01-01
      • 2011-01-14
      • 1970-01-01
      • 2015-11-24
      • 2011-03-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多