Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåDenne nettleseren støttes ikke lenger.
Oppgrader til Microsoft Edge for å dra nytte av de nyeste funksjonene, sikkerhetsoppdateringene og den nyeste tekniske støtten.
This article describes how you can use the .NET Multi-platform App UI (.NET MAUI) IPreferences
interface. This interface helps store app preferences in a key/value store.
The default implementation of the IPreferences
interface is available through the Preferences.Default
property. Both the IPreferences
interface and Preferences
class are contained in the Microsoft.Maui.Storage
namespace.
Preferences are stored with a String key. The value of a preference must be one of the following data types:
Values of DateTime
are stored in a 64-bit binary (long integer) format using two methods defined by the DateTime
class:
ToBinary
method is used to encode the DateTime
value.FromBinary
method decodes the value.See the documentation of these methods for adjustments that might be made to decoded values when a DateTime
is stored that isn't a Coordinated Universal Time (UTC) value.
Preferences are set by calling the Preferences.Set
method, providing the key and value:
// Set a string value:
Preferences.Default.Set("first_name", "John");
// Set an numerical value:
Preferences.Default.Set("age", 28);
// Set a boolean value:
Preferences.Default.Set("has_pets", true);
To retrieve a value from preferences you pass the key of the preference, followed by the default value when the key doesn't exist:
string firstName = Preferences.Default.Get("first_name", "Unknown");
int age = Preferences.Default.Get("age", -1);
bool hasPets = Preferences.Default.Get("has_pets", false);
In some scenarios you may need to pass the key of the preference, followed by the default value and its type:
long value = Preferences.Get("master_date", (long)0);
It may be useful to check if a key exists in the preferences or not. Even though Get
has you set a default value when the key doesn't exist, there may be cases where the key existed, but the value of the key matched the default value. So you can't rely on the default value as an indicator that the key doesn't exist. Use the ContainsKey
method to determine if a key exists:
bool hasKey = Preferences.Default.ContainsKey("my_key");
Use the Remove
method to remove a specific key from preferences:
Preferences.Default.Remove("first_name");
To remove all keys, use the Clear
method:
Preferences.Default.Clear();
The preferences stored by your app are only visible to your app. However, you can also create a shared preference that can be used by other extensions or a watch app. When you set, remove, or retrieve a preference, an optional string parameter can be supplied to specify the name of the container the preference is stored in.
The following methods take a string parameter named sharedName
:
Preferences.Set
Preferences.Get
Preferences.Remove
Preferences.Clear
Viktig
Please read the platform implementation specifics, as shared preferences have behavior-specific implementations
Preferences are stored natively, which allows you to integrate your settings into the native system settings. Follow the platform documentation to integrate with the platform:
This section describes the platform-specific differences with the preferences API.
All data is stored into Shared Preferences. If no sharedName
is specified, the default Shared Preferences are used. Otherwise, the name is used to get a private Shared Preferences with the specified name.
Uninstalling the application causes all preferences to be removed, except when the app runs on Android 6.0 (API level 23) or later, while using the Auto Backup feature. This feature is on by default and preserves app data, including Shared Preferences, which is what the Preferences API uses. You can disable this by following Google's Auto Backup documentation.
Performance may be impacted if you store large amounts of text, as the API was designed to store small amounts of text.
.NET MAUI-tilbakemelding
.NET MAUI er et åpen kilde-prosjekt. Velg en kobling for å gi tilbakemelding:
Hendelser
17. mars, 21 - 21. mars, 10
Bli med i meetup-serien for å bygge skalerbare AI-løsninger basert på virkelige brukstilfeller med andre utviklere og eksperter.
Registrer deg nåOpplæring
Modul
Lagre lokale data med SQLite i en .NET MAUI-app - Training
Lær hvordan du lagrer og får tilgang til data i SQLite ved hjelp av en MAUI-app (Multi-Platform App UI)
Dokumentasjon
File system helpers - .NET MAUI
Learn how to use the .NET MAUI IFileSystem interface in the Microsoft.Maui.Storage namespace. This interface contains helper methods that access the application's cache and data directories, and helps open files in the app package.
Learn how to use the .NET MAUI ISecureStorage interface, which helps securely store simple key/value pairs. This article discusses how to use the ISecureStorage, platform implementation specifics, and its limitations.
Learn how to use the .NET MAUI IFilePicker interface in the Microsoft.Maui.Storage namespace, which lets a user choose one or more files from the device.