Xamarin Forms Saving Data without SQL

Xamarin Forms Saving Data wirhout SQL any SQL SQLite SavableObject Plugin Xamarin, Xamarin Froms save data
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

enis necipoglu xamarin forms savableobject data save no sql
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.

savableObject-nuget enis necipoglu visual studio nuget Xamarin - Xamarin Forms

 

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:

<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
            xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
            xmlns:local="clr-namespace:TestAppSavableObject"
            x:Class="TestAppSavableObject.MainPage">
    <ContentPage.BindingContext>
        <local:MainPageViewModel/>
    </ContentPage.BindingContext>
    <ContentPage.Content>
        <StackLayout>
            <Entry Text="{Binding Name,Mode=TwoWay}"/>
            <Entry Text="{Binding Surname,Mode=TwoWay}"/>
            <Stepper Value="{Binding Age,Mode=TwoWay}"/>
            <Switch IsToggled="{Binding IsToggled,Mode=TwoWay}"/>

            <Button Text="Save" Command="{Binding SaveCommand}"/>
        </StackLayout>
    </ContentPage.Content>
</ContentPage>

Xamarin Forms Savable Object allows Save data withouw sql in Xamarin Forms
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 Plugin.LocalData.Abstractions;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;

namespace TestAppSavableObject
{
    public class MainPageViewModel : SavableObject, INotifyPropertyChanged
    {
        public MainPageViewModel()
        {
            Load();
            SaveCommand = new Command(()=>Save());
        }

        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:

Load();
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 NullableApplication 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 added

label, , , , ,

About the author

Piksel Akademi | Xamarin Developer "İş olarak kod yazan, hobi olarak kod yazan biriyim. Unity 3D ve Xamarin Forms platformlarında C# ile uygulamalar yazmaktan zevk alırım. .Net Framework'ün taşınabildiği her platfromda da çalışma gerçekleştirmiş olmayı severim."

Add a Comment

E-posta hesabınız yayımlanmayacak. Gerekli alanlar * ile işaretlenmişlerdir