Xamarin Forms Sharedpreferences交叉 [英] Xamarin Forms Sharedpreferences cross

查看:104
本文介绍了Xamarin Forms Sharedpreferences交叉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道以跨平台方式操纵应用程序设置的最佳解决方案是什么.

I'd like to know what is the best solution to manipulate application settings in a cross-platform way.

在iOS中,我们可以在设置屏幕中更改应用程序外部的设置,但Windows Phone和android中没有此设置.

In iOS we can change the settings outside the app in the settings screen, but we don't have that in windows phone and android.

因此,我的想法是在应用程序内部创建一个正常的页面/屏幕,以显示我的所有应用程序设置,并具有Save()和Get()方法的接口,我可以使用DependencyServices在每个设备上实现特定的操作.

So, my idea is to create a normal page/screen inside the app that shows all my application settings and have an interface with Save() and Get() methods that I can implement specific per device using DependencyServices.

这是正确的方法吗?

推荐答案

  1. Application子类具有可用于存储数据的静态属性"字典.可以使用Application.Current.Properties从Xamarin.Forms代码中的任何位置进行访问.

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

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

属性"字典将自动保存到设备.当应用程序从后台返回或重新启动后,添加到字典中的数据将可用. Xamarin.Forms 1.4在Application类上引入了另一个方法-SavePropertiesAsync()-可以调用该方法来主动保留Properties字典.这样一来,您就可以在重要更新后保存属性,而不用冒着崩溃或被操作系统杀死而使它们不被序列化的风险.

The Properties dictionary is saved to the device automatically. Data added to the dictionary will be available when the application returns from the background or even after it is restarted. Xamarin.Forms 1.4 introduced an additional method on the Application class - SavePropertiesAsync() - which can be called to proactively persist the Properties dictionary. This is to allow you to save properties after important updates rather than risk them not getting serialized out due to a crash or being killed by the OS.

https://developer.xamarin.com/guides/跨平台/xamarin-forms/working-with/app-lifecycle/

  1. 使用本机设置管理的Xamarin.Forms插件.

  1. Xamarin.Forms plugin which uses the native settings management.

  • Android:SharedPreferences
  • iOS:NSUserDefaults
  • Windows Phone:IsolatedStorageSettings
  • Windows应用商店/Windows Phone RT:ApplicationDataContainer

https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings

这篇关于Xamarin Forms Sharedpreferences交叉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆