Winner's Excogitations

A chronicle of the thoughts, learning experiences, ideas and actions of a tech junkie, .NET, JS and Mobile dev, aspiring entrepreneur, devout Christian and travel enthusiast.

Xamarin Forms: Handling User Preferences

5 years ago ยท 3 minutes read

bolorundurowb_com/production/article/qq3nby4knv7f054jihiq

Introduction

As you build a mobile app, you may want to save small bits of user data and/or preferences usually as a key-value pair. For a while, there was no Xamarin cross-platform way of getting that done, so platform-specific code had to be written. With the introduction of Xamarin Essentials, a number of commonly accessed functionality has been made cross-platform. In this guide, we'll walk through persisting some user data in a Xamarin Forms application. NOTE: If you want to save sensitive data, use Secure Storage instead.

Setup

Using your favourite editor, create a Xamarin Forms project. I am using JetBrains Rider as my IDE and naming my project xamarin-forms.

Installing the library

To make use of the Xamarin Essentials library, it has to be installed in the PCL/NET Standard project as well as any platform-specific projects. For my own IDE, this is the view:

bolorundurowb_com/production/article/ff1puegtxotnymzqvu7t

After the installation is complete, if there is an Android target project then we need to update the MainActivity.cs file (or any other Activity file that is the launch target) with the following:

...
base.OnCreate(savedInstanceState);
// add the line below to your file
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
...

Persisting data

For this example, I want to save the current Date and Time in the user's preferences when I call a method. The logic for my method is shown below:

using Xamarin.Essentials;
...
public void SaveToPreferences()
{
    Preferences.Set("app_CurrentDate", DateTime.UtcNow);
}

In this case, we saved a DateTime type to the preferences but the library has overloads to support a number of primitives including bool, double, int, float, long and string.

Retrieving saved data

To retrieve the data I persisted, I would add in a method to get the date I saved. The method is shown below:

protected DateTime ReadSavedPreference()
{
    var date = Preferences.Get("app_CurrentDate", DateTime.MinValue);
    return date;
}

The more astute of you would have noticed the DateTime.MinValue values provided to the Get method. That is the default that the method returns if the provided key does not exist. For our example, if we have not saved a value to the app_CurrentDate key, then the default .NET minimum DateTime value (1/01/0001 0:00:00 AM) would be returned.

Cleaning up

If at any point we no longer need the data tied to a certain key, we can remove the data. In the context of the data, we have saved in the previous examples. if we were to create a method to remove the date we saved, the method implementation should look like :

public void RemoveSavedDate()
{
    Preferences.Remove("app_CurrentDate");
}

If we want to remove all saved app preferences, the Xamarin Essentials library provides us with a handy method to accomplish that as well.

Preferences.Clear();

Till the next one,

Cheers

Share on:
We Are Not Victims!!!
A treatise on why our mindsets need to change.
Xamarin Forms: How To Use Custom Fonts
How to use custom fonts in a Xamarin Forms app
Winner-Timothy Bolorunduro
Winner-Timothy Bolorunduro is a senior .NET developer with over 6 years experience helping organizations and individuals build compelling, stable and scalable web applications. Having spent the last three years in a fast-paced startup environment working remotely, he understands what goes into being part of a team that produces value for clients.

Comments