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

Improving C# Performance by Using AsSpan and Avoiding Substring

During development and everyday use, Substring is often the go-to choice for string manipulation. However, there are cases where Substring c...

MSDEVBUILD - English Channel

MSDEVBUILD - Tamil Channel

Popular Posts