The best way to save Configuration data in Xamarin

2019-07-27 22:13发布

问题:

I am now working on an Xamarin.Forms based app, and have few questions about this. Is there a best way for me to just save some configuration data? As I am using an MVVM based app in like this

private bool _isActive;
public bool IsActive
{
   get { return _isActive; }
   set
   {
      _isActive = value;
      RaisePropertyChanged(nameof(IsActive))
   }
}

I wanted to save the "IsActive" value when use it other time I load my app.
So is there a best way for me to do this?

回答1:

I would currently advise against using the Application.Properties feature of Xamarin Forms, as it is not particularly reliable nor resilient. Instead, I would recommend using the cross-platform settings plugin (On NuGet: https://www.nuget.org/packages/Xam.Plugins.Settings/ and source code / docs: https://github.com/jamesmontemagno/Xamarin.Plugins/tree/master/Settings), which provides just as simple of an API but without the problems of the Application.Properties feature.

The settings plugin also has some additional benefits such as integrating with the native app settings (so you can surface the same values in the iOS Settings app) as well as being a synchronous API (you don't need to do any Async calls to save or read the data values).



回答2:

The Xamarin Forms Application class has a built in cross platform way to save settings data like this, probably the easiest way unless you need something more. Take a look at Application.Properties and Application.SavePropertiesAsync