Introduction

One of the most important tasks when we build an application is to allow the user to save data. We all know that each platform has a different implementation to do that. When we use Xamarin with the .Forms UI Technology and the PCL (Portable Class Library) code sharing strategy, we could get confused about writing, reading, and saving data implementation.

The goal of this tutorial is to show you how to write, read, and save data using Xamarin.Forms.

There are four “places” there to save data:

  1. Preferences or Application Properties: Great to save the using preferences of the user.
  2. File System: Great to save most types of data
  3. Database: Great when we need to allow the user to do relational operations with the data.
  4. Cloud: When we need to connect to a web service

In this tutorial series, I’ll cover the all four methods for the three mobile platforms and Windows desktop.

It’s important to say, before starting, that I’ll use “basic code method” and not the “clean code” one, because the purpose of the present tutorial is to show you the basics to save data.

Prerequisites

The steps given below will lead you to the goal.

Part 1

Preferences or Application Properties

Step 1

Step 2

Before

Visual Studio

After

Visual Studio

Step 3

Before

Visual Studio

After

Visual Studio

Step 4

Step 5

Windows 10 Desktop

Launch application.

Visual Studio

Insert the user’s preferences.

Visual Studio

Restart the app. We’ll get the following output.

Visual Studio

Windows 10 Mobile

Launch application.

Visual Studio

Insert the user’s preferences.

Visual Studio

Restart the app. We’ll get the following output.

Visual Studio

Android

Launch application.

Visual Studio

Insert the user’s preferences.

Visual Studio

Restart the app. We’ll get the following output.

Visual Studio

iOS

Launch application.

Visual Studio

Insert the user’s preferences.

Visual Studio

Restart the app. We’ll get the following output.

Visual Studio

Conclusion

As we saw before, even if the application has been closed, the preferences are active and reflect the users’ settings when it restarts. This method is great when we need to allow the user to use and store settings preferences about the app.

As I said earlier, the code shown above is essentially the basics, just like the purpose of this tutorial. Of course, you can improve it avoiding the redundant and hard code, but that is out of aim at this time.

In the next tutorial, I’ll show you how to save data in the File System.

Thank you for your attention and interest.