Xamarin Essentials Preferences
Xamarin.Essentials provides a way to store key-value data in the specific platform preferences.
The preference API is accessible from any shared code including shared projects, PCLs, and .NET Standard Libraries.
Syntax
Preferences.Get(string key, T defaultValue);
Preferences.Set(string key, T value);
Preferences.ContainsKey(string key);
Preferences.Remove(string key);
Preferences.Clear();
Example
public void SaveToPreferences()
{
Preferences.Set("key", "value");
}
public string LoadFromPreferences()
{
string preference = "";
if (Preferences.ContainsKey("key"))
preference = Preferences.Get("key", "");
return preference;
}
Output
We can store and retrieve data from the application preferences.
Explanation
Xamarin.Essentials Preferences provides a simple programming model for storing and retrieving preferences.
The API is available from any shared code, including shared projects and .NET Standard Libraries.
You can save and retrieve all the primitives data types (Boolean, Float, Double, Int32, Int64, and String) safely.
Other types can be handled using serialization.
Use
We can use Xamarin.Essentials Preferences to store and retrieve application-specific data.
It is often used to store user preferences, session-specific data that must be preserved between application launches, or other key-value related data.
Summary
Xamarin.Essentials Preferences provides a simple programming model for storing and retrieving application-specific data.
It is often used to store user preferences, session-specific data that must be preserved between application launches, or other key-value related data.