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

Microsoft Azure: Configure Azure App Service Logging

Azure App service logging are the output of runtime trace statement in the different app code. logging feature will help you might only want...

Popular Posts