如何在Xamarin.Forms中存储应用程序设置

38

我们如何在Xamarin.Forms中将App设置以键值对的形式存储和检索? 例如,当应用程序关闭时,我们可以存储用户首选项,在重新启动应用程序时,我们能够获取这些值。


https://forums.xamarin.com/discussion/41680/application-current-properties-can-not-persist-in-android-using-xf-1-42 - Akash Amin
5个回答

56

2
请注意,您不应将此用于用户设置。这些属性被序列化到一个xml文件中,并不使用底层平台的本地设置/首选项机制。例如,在iOS中,它不会将项目存储在NSUserDefaults中。您将无法从系统的设置包中修改它(https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/UserDefaults/Preferences/Preferences.html)。 - LanderV
4
Application.Current.Properties 更多关注在跨活动中保留应用程序状态(OnStart/OnSleep/OnResume)。 - LanderV

8

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


1
对于那些想知道为什么他们的代码不能保留所设置的属性的人,你需要使用上面的方法来保存它们!我在这个问题上纠结了很久。谢谢! - phantomraa

8
我使用Xamarin和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 - Akash Amin
1
@AkashAmin 我目前几乎所有的应用程序都使用了Akavache(用于缓存,但正在考虑转移到Realm以提高速度并为未来项目删除SQLite依赖)。但是对于存储一些应用程序设置,它有点过头了... - SushiHangover
太好了。我也在研究 Realm。不过还是谢谢你的反馈。 - Akash Amin
是的,期待着。谢谢。@SushiHangover - Akash Amin
@batmaci 这个插件与https://developer.xamarin.com/guides/xamarin-forms/working-with/application-class/#Properties_Dictionary不同。 - SushiHangover
显示剩余5条评论

4
如果设置更多的是配置项,不需要在运行时更改(例如应用程序的 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"];

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://learn.microsoft.com/zh-cn/xamarin/essentials/preferences?tabs=android


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接