Store Data and Access from any Pages in xamarin.Form

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), resime(OnResume) method. I have tried with application crashes out with an exception properties it’s not saved.

Xamarin.Forms plugin which uses the native settings management. Refer below image different platform state persistence.

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

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

Application different Action:

Refer below code for different xamarin Form application property action 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"] = "jssuthahar@gmail.com";

Get Value:

While getting the value, check if its available or not and always convert object type into your specific variable type.

if (Application.Current.Properties.ContainsKey("Email"))
{
var emailId = Convert.ToString(Application.Current.Properties["Email"]);
}
If you want to reuse the same key value or update value. just assign value like below
Application.Current.Properties["Email"] = "nikhil@msdn.com";

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 below code
if (Application.Current.Properties.ContainsKey("Email"))
{
Application.Current.Properties.Remove("Email");
}

Clear Value:

You can clear all the application property value
Application.Current.Properties.Clear();

If you have question type(?) ask in Comments Box

0 Comments

Featured Post

Understanding about Azure Region, Geographies, Availability Zones and Region Pairs

Introduction Microsoft Azure services are available in 53 regions around the world, with plans announced for 7 additional regions and 140+ a...

Subscribe to the FREE Weekly Newsletter to receive all the latest Microsoft Azure, AI, Xamarin

Popular Posts