Storing user preferences
Most apps allow a user to set preferences that can be stored on a phone and retrieved, typically when the app starts. .NET MAUI provides a service for this, easily storing key/value pairs, such as theme preferences, the last date used, the login name, and so on.
.NET MAUI provides the IPreferences
interface to help store these preferences. With this, and the associated Preferences
class (both in the Microsoft.Maui.Storage
namespace), you can store string keys and values of any of the following types:
Boolean
Double
Int
(int32
,single
, andint64
)String
DateTime
Persisting DateTime
DateTime
values are stored as 64-bit integers and use the ToBinary
and FromBinary
methods to encode and decode respectively.
Let’s create a UserPreferences
page with a short form to gather a user’s preferences. We’ll also add Button
, which will display all the saved preferences and allow the user to delete one or all of...