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"] = "";

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"] = "";

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"))

Clear Value:

You can clear all the application property value

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


Featured Post

Power Virtual Agents: customize the bot conversation messages

Power Virtual Agents are an easily created powerful bot and no-code graphical interface without the need for data scientists or developers. ...

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

Popular Posts