Store Data And Access It From Any Page In Xamarin.Form

Introduction

The State Persistence between app restarts and when suspending/resuming: The values in the properties dictionary are only stored when the app goes to Start(OnStart), sleep(OnSleep), resume(OnResume) method. I have tried; when the application crashes out with exception properties it’s not saved.

Xamarin.Forms plugin uses the native settings management. Refer to the below image for different platform state persistences.


This can be accessed from anywhere in your Xamarin.Forms code using Application.Current.Properties .The syntax looks like below.

  1. Application.Current.Properties [ <StringKey> ] = <Assign Object Type Value >;  
Application different Action

Refer to the below code for different Xamarin Form application property actions like assign, read, delete, Clear value.

Set value

Key value always should be string and Value should be object (any type)

Application.Current.Properties["Email"] = "[email protected]";

Get Value

While getting the value, check if it's available or not and always convert object type into your specific variable type.
  1. if (Application.Current.Properties.ContainsKey("Email"))  
  2. {  
  3.    var emailId = Convert.ToString(Application.Current.Properties["Email"]);  
  4. }  
If you want to reuse the same key value or update value, just assign value like below.
  1. Application.Current.Properties["Email"] = "[email protected]";  
The properties dictionary can only serialize primitive types for storage. Attempting to store other types like,

List<int> can fail silently

Remove Value

If you want to remove application key and value, try the below code.
  1. if (Application.Current.Properties.ContainsKey("Email"))  
  2. {  
  3.    Application.Current.Properties.Remove("Email");  
  4. }  
Clear Value

You can clear all the application property value s:
  1. Application.Current.Properties.Clear();  
If you have questions ask in the comments.