Xamarin, Xamarin Essentials

Touching base with Preferences in Xamarin Forms

Sometimes we can face situations where we will have to keep some important information stored that we want to always have at hand to use in our application. ?

For example, our App may have a login that you access with your email. For a best user experience we can provide an email remembering option instead of writing it every time. To store this email we use the Preferences! This login is just the example we will be creating in this article!


Let’s start!

What are Preferences? ?

Preferences class store application preferences in a key/value store.  (Xamarin Essentials gives us ?)

Supported data values

First of all, It’s important to consider which data values are accepted when you are handling the values that can be Get or Set in the Preference keys.

This image has an empty alt attribute; its file name is PreferencesAcceptedValues-1024x385.png


Setting preferences

To set some Preference you just have to call the Preferences class followed with the Set method, it takes the following parameters:

➖ Key name: Receives the name which you want to identify the Preference.

➖ Key value: Receives the value that will be have the key preference.

Implementing in code is just like this:


Getting preferences

To get a Key value you have yo call with the same key name that you created a key Preference. Using Get method takes the following parameters:

➖ Key name: Receives the name with the key name that you want to get the value.

➖ Key default value: Receives a default value specified if the key does don’t exists or you just can set it in blank.

Implementing in code is just like this:


Removing key preferences

➖ To remove an specific key: Just set the name of the key that you want to remove.

➖ To remove all the Preferences:


And now, let’s do an example!

In the following example we want to do a login App that needs to remember the user email. (Regardless of whether the applications are closed, our cell phone must remember).

Creating the XAML

Working with the events!

And finally, the result ?

In the first screen we can see remembering the email and in the second screen we can see that when the App is closed the information is stored yet! ?

This image has an empty alt attribute; its file name is PreferfencesPSample.gif

Thanks for reading!!! ??

Spanish post: https://medium.com/@reyes.leomaris/trabajando-con-preferencias-en-xamarin-forms-3f2a73494583

References: https://docs.microsoft.com/en-us/xamarin/essentials/preferences?tabs=android

Tagged , ,

1 thought on “Touching base with Preferences in Xamarin Forms

Leave a Reply

Your email address will not be published. Required fields are marked *