Xamarin.Forms - Working With Application Storage

Introduction

Xamarin.Forms code runs on multiple platforms -- each of which has its own filesystem. This means that reading and writing files are the most easily done tasks using native file APIs on each platform. Alternatively, embedded resources are also a simpler solution to distribute the data files with an app.

 
Application Storage

Xamarin Android - Shared Preferences

Android provides many ways of storing the data of an application. One of the ways is called Shared Preferences. Shared Preferences allow you to save and retrieve data in the form of key, value pair.

In order to use shared preferences, you have to call a method getSharedPreferences() that returns a SharedPreference instance pointing to the file that contains the values of preferences.

Xamarin iOS - NSUserDefaults

The NSUserDefaults class provides a way for iOS Apps and Extensions to programmatically interact with the system-wide Default System. By using the Defaults System, the user can configure an app's behavior or styling to meet their preferences (based on the design of the app). For example, to present data in Metric vs Imperial measurements or select a given UI Theme.

UWP - ApplicationDataContainer

Represents a container for app settings. The methods and properties of this class support creating, deleting, enumerating, and traversing the container hierarchy.

Xamarin Forms - Properties Dictionary

The Application subclass has a static Properties dictionary which can be used to store data, in particular for use in the OnStart, OnSleep, and OnResume methods. This can be accessed from anywhere in your Xamarin.Forms code using Application.Current.Properties.

The Properties dictionary uses a string key and stores an object value.

Prerequisites
  • Visual Studio 2017(Windows or Mac)
Setting up a Xamarin.Forms Project

Start by creating a new Xamarin.Forms project. You’ll learn more by going through the steps yourself.

Choose the Cross-platform App project under Visual C#-->Cross-platform in the New Project dialog.

 

Now Select the Blank App and Choose Portable Class Library (PCL).

 
 
You now have a basic Xamarin.Forms app. Click the Play button to try it out.


Setting up the User Interface.

Go to MainPage.Xaml and write the following code.

MainPage.Xaml
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:XamarinFormsAStorage"  
  5.              x:Class="XamarinFormsAStorage.MainPage">  
  6.     <ContentPage.Content>  
  7.         <StackLayout>  
  8.         <StackLayout>  
  9.             <Label Text="Application Storage" FontSize="Medium" HorizontalTextAlignment="Center"></Label>  
  10.             <Entry x:Name="txtId" Placeholder="Enter id"></Entry>  
  11.             <Entry x:Name="txtName" Placeholder="Enter Name"></Entry>  
  12.             <Switch x:Name="switch1" IsToggled="False" HorizontalOptions="Center"></Switch>  
  13.             <Button x:Name="btnStore" Text="Store"></Button>  
  14.             <Button x:Name="btnGet" Text="Get"></Button>  
  15.             </StackLayout>  
  16.         <StackLayout>  
  17.                 <Label x:Name="lblId" Text="ID" HorizontalTextAlignment="Center"></Label>  
  18.                 <Label x:Name="lblName" Text="Name" HorizontalTextAlignment="Center"></Label>  
  19.                 <Label x:Name="lblIsMVP" Text="MVP" HorizontalTextAlignment="Center"></Label>  
  20.                 <Button x:Name="btnClear" Text="Clear"></Button>  
  21.                 <Button x:Name="btnRemove" Text="Remove"></Button>  
  22.             </StackLayout>  
  23.         </StackLayout>  
  24.     </ContentPage.Content>  
  25. </ContentPage>  
 
 
 
 
Store Value

The Properties dictionary uses a string key and stores an object value.

Now, write the following code on the BtnStore_Clicked event.

MainPage.Xaml.cs
  1. private void BtnStore_Clicked(object sender, EventArgs e)  
  2.         {  
  3.             // Store all  Values  
  4.             Application.Current.Properties["ID"] = txtId.Text;  
  5.             Application.Current.Properties["Name"] = txtName.Text;  
  6.             Application.Current.Properties["IsMVP"] = switch1.IsToggled;  
  7.             txtId.Text = string.Empty;  
  8.             txtName.Text = string.Empty;  
  9.             DisplayAlert("Success""All Vaues stored""OK");  
  10.               
  11.         }  

 
 

Get Value

The Properties dictionary stores objects so you need to cast its value before using it.

Now, write the following code on the BtnGet_Clicked event.

MainPage.Xaml.cs
  1. private void BtnGet_Clicked(object sender, EventArgs e)  
  2.         {  
  3.             //Get all Values  
  4.             if (Application.Current.Properties.ContainsKey("ID"))  
  5.             {  
  6.                 lblId.Text = Application.Current.Properties["ID"].ToString();  
  7.                 lblName.Text = Application.Current.Properties["Name"].ToString();  
  8.                 lblIsMVP.Text = Application.Current.Properties["IsMVP"].ToString();  
  9.             }  
  10.               
  11.         }  
 
 

Remove Value

If you want to remove values, you can remove them.

Now, write the following code on the BtnRemove_Clicked event.

MainPage.Xaml.cs
  1. private void BtnRemove_Clicked(object sender, EventArgs e)  
  2.         {  
  3.             //Remove all Properties  
  4.             if (Application.Current.Properties.ContainsKey("ID"))  
  5.             {  
  6.                 Application.Current.Properties.Remove("ID");  
  7.                 Application.Current.Properties.Remove("Name");  
  8.                 Application.Current.Properties.Remove("IsMVP");  
  9.                 ClearAll();  
  10.                 DisplayAlert("Success""All Vaues Removed""OK");  
  11.             }  
  12.         }  


Clear Values

If you want clear all Properties, you can use the following code.

Now, write the following code on the BtnClear_Clicked event.

MainPage.Xaml.cs
  1. private void BtnClear_Clicked(object sender, EventArgs e)  
  2.         {  
  3.             //Clear all Properties  
  4.             Application.Current.Properties.Clear();  
  5.             ClearAll();  
  6.             DisplayAlert("Success""All Vaues Cleared""OK");  
  7.         }  
 

 
 
Full Code - MainPage.Xaml.cs
  1. namespace XamarinFormsAStorage  
  2. {  
  3.     public partial class MainPage : ContentPage  
  4.     {  
  5.         public MainPage()  
  6.         {  
  7.             InitializeComponent();  
  8.   
  9.             btnStore.Clicked += BtnStore_Clicked;  
  10.             btnGet.Clicked += BtnGet_Clicked;  
  11.             btnRemove.Clicked += BtnRemove_Clicked;  
  12.             btnClear.Clicked += BtnClear_Clicked;  
  13.             switch1.Toggled += Switch1_Toggled;  
  14.         }  
  15.   
  16.         private void BtnClear_Clicked(object sender, EventArgs e)  
  17.         {  
  18.             //Clear all Properties  
  19.             Application.Current.Properties.Clear();  
  20.             ClearAll();  
  21.             DisplayAlert("Success""All Vaues Cleared""OK");  
  22.         }  
  23.   
  24.         private void BtnRemove_Clicked(object sender, EventArgs e)  
  25.         {  
  26.             //Remove all Properties  
  27.             if (Application.Current.Properties.ContainsKey("ID"))  
  28.             {  
  29.                 Application.Current.Properties.Remove("ID");  
  30.                 Application.Current.Properties.Remove("Name");  
  31.                 Application.Current.Properties.Remove("IsMVP");  
  32.                 ClearAll();  
  33.                 DisplayAlert("Success""All Vaues Removed""OK");  
  34.             }  
  35.         }  
  36.   
  37.         private void Switch1_Toggled(object sender, ToggledEventArgs e)  
  38.         {  
  39.             Application.Current.Properties["IsMVP"] = switch1.IsToggled;  
  40.         }  
  41.   
  42.         private void BtnGet_Clicked(object sender, EventArgs e)  
  43.         {  
  44.             //Get all Values  
  45.             if (Application.Current.Properties.ContainsKey("ID"))  
  46.             {  
  47.                 lblId.Text = Application.Current.Properties["ID"].ToString();  
  48.                 lblName.Text = Application.Current.Properties["Name"].ToString();  
  49.                 lblIsMVP.Text = Application.Current.Properties["IsMVP"].ToString();  
  50.             }  
  51.               
  52.         }  
  53.   
  54.         private void BtnStore_Clicked(object sender, EventArgs e)  
  55.         {  
  56.             // Store all  Values  
  57.             Application.Current.Properties["ID"] = txtId.Text;  
  58.             Application.Current.Properties["Name"] = txtName.Text;  
  59.             Application.Current.Properties["IsMVP"] = switch1.IsToggled;  
  60.             txtId.Text = string.Empty;  
  61.             txtName.Text = string.Empty;  
  62.             DisplayAlert("Success""All Vaues stored""OK");  
  63.               
  64.         }  
  65.   
  66.         public void ClearAll()  
  67.         {  
  68.             lblId.Text = string.Empty;  
  69.             lblName.Text = string.Empty;  
  70.             lblIsMVP.Text = string.Empty;  
  71.         }  
  72.     }  
  73. }  
Click the Play button to try it out.
 
 
 
 
I hope you have understood how to use Application Storage in Xamarin Forms.
 
Thanks for reading. Please share comments and feedback.
G
M
T
 
Text-to-speech function is limited to 200 characters


Similar Articles