Hi Everyone,
Xamarin Forms Android SQLite Problem
As most of you knows SQLite has some problems in Android 7 Nougat. And I’ve presented you a different method to save your data to local storage in device.
The Logic of my Plugin
Firsly,
We should know Application.Current.Properties is a dictionary in Xamarin Forms which is able to save to device. You can add a value with using, Application.Current.Properties.Add(“MyData”,”Hello, My name is Enis!”); and when you use Application.Current.SavePropertiesAsync() that dictionary saves all values with keys. And you can call them later like Application.Current.Properties[“MyData”] . UUgh. It’s easy way to save little datas. But if you have too much data or some Collecton to save. It’s not usable! Don’t worry, it’s possible but it’s too complicated. That’s why I’ve written some class to manage Application Properties. Your models will be saved in Xamarin Forms Layer with its properties via using Save() method.That means it works for every platform. And than it’ll be restored when you call Load() method (Mostly in constructor.)
Let’s make an sample
Firstly you should download the plugin which named as Xamarin.Forms.SavableObject and you can find my plugin’s source codes from this Git.Hub link. Plugin should be installed only Portable Layer. It’s enough to work. Most important dependency is Newtonsoft.JSON v.9.0.1, if you don’t have, please install that package first.
Before start coding. I’ve not supported any pattern except MVVM. It’ll works any pattern successfully, but it’s not easy to use with as MVVM.
We’ll save 2 string, 1 double and 1 bool parameter on locally. Design is not important in that sample 🙂 Place 2 Entry, 1 Stepper, 1 Label and 1 switch in your ContentPage (I’ve used MainPage for that which comes default on a new project).
Our MainPage.XAML will be like that:
<label></label>
<button></button>
And we need a ViewModel. Just Create a cs file to root of project and name it MainPageViewModel.cs. After created and Implementing some MVVM things, ViewModel will be like that:
using System.Runtime.CompilerServices;
using Xamarin.Forms;
namespace TestAppSavableObject
{
public class MainPageViewModel : SavableObject, INotifyPropertyChanged
{
public MainPageViewModel()
{
}
string name = "defaultName", surname = "defaultSurname";
double age;
bool isToggled;
public string Name
{
get => name;
set { name = value; OnPropertyChanged(); }
}
public string Surname
{
get => surname;
set { surname = value; OnPropertyChanged(); }
}
public double Age
{
get => age;
set { age = value; OnPropertyChanged(); }
}
public bool IsToggled
{
get => isToggled;
set { isToggled = value; OnPropertyChanged(); }
}
[IgnoreSave]
public Command SaveCommand { get; set; }
#region INotifyPropertyChanged Implementation
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged([CallerMemberName] string propertyName = "")
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
#endregion
}
}
Everything is normal until here! Just Inherit from SavableObject and see the Magic! Add “using Plugin.LocalData.Abstractions;” and add Load() method in constructor. Then set SaveCommands value:
SaveCommand = new Command(()=>Save());
Everything is ready. When this class Instaced ones, datas will be read and restored to your properties. When you use Save() method, all properties in class will be saved. If you want to not save some properties like Command in my situation, just add [IgnoreSave] attribute to that property. Everything is OK!.
If you want to work with Collections, you can see the sample project from here:
https://github.com/enisn/TestAppSavableObject
Supported Situations:
- All value type properties can be saved,
- All the strings (Length is not important) can be saved,
- All Collections, which Inherits from IEnumarable can be saved
WARNING!
If one of your property is a Nullable, Application Properties will be have a bug and you’ll not be able to save or load any data! If you have Nullable<> something or something with ? that class proeprties will not be saved or loaded!
Object support will be added in later versions.