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.
- 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.
- 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.
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.
- if (Application.Current.Properties.ContainsKey("Email"))
- {
- Application.Current.Properties.Remove("Email");
- }
Clear Value
You can clear all the application property value s:
- Application.Current.Properties.Clear();
If you have questions ask in the comments.