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

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

问题描述

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

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

在 iOS 中,我们可以在设置屏幕中更改应用程序外的设置,但在 windows 手机和 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:共享首选项
  • iOS:NSUserDefaults
  • Windows Phone:IsolatedStorageSettings
  • Windows 应用商店/Windows Phone RT:ApplicationDataContainer

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

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

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

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