Persistent Services
The most powerful feature of Stores. Register an INotifyPropertyChanged class as a persistent service, and every property change is automatically written to the backing store. When the app restarts, the object is rehydrated from the store.
-
Create a class that implements
INotifyPropertyChanged:[Reflector] // Optional but recommended — removes reflection for AOT safetypublic partial class AppSettings : INotifyPropertyChanged{string theme = "light";public string Theme{get => theme;set { theme = value; OnPropertyChanged(); }}bool notificationsEnabled = true;public bool NotificationsEnabled{get => notificationsEnabled;set { notificationsEnabled = value; OnPropertyChanged(); }}// INotifyPropertyChanged implementationpublic event PropertyChangedEventHandler? PropertyChanged;void OnPropertyChanged([CallerMemberName] string? name = null)=> PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(name));} -
Register as a persistent service:
// Uses the default "settings" storebuilder.Services.AddPersistentService<AppSettings>();// Or target a specific storebuilder.Services.AddPersistentService<SecureSettings>("secure"); -
Inject and use — changes are persisted automatically:
public class SettingsViewModel(AppSettings settings){public AppSettings Settings => settings;public void ToggleNotifications(){// This change is automatically persisted to the storeSettings.NotificationsEnabled = !Settings.NotificationsEnabled;}}