| MAUI-CommunityToolkit-7 :👈 | 👉:MAUI-CommunityToolkit-9 |
Hour 8 β Data Persistence (Preferences, SecureStorage, SQLite, File I/O) |
Welcome to Hour 8 of your MAUI MVVM series. Today we cover how MAUI apps store and load data using:
Preferences are used to save small data such as settings, flags, or strings.
Preferences.Set("username", "john");
Preferences.Set("isLoggedIn", true);
string user = Preferences.Get("username", "Guest");
bool logged = Preferences.Get("isLoggedIn", false);
Preferences.Remove("username");
Preferences.Clear();
β Use Preferences for nonβsensitive settings.
SecureStorage encrypts data using device-level protection. Use it for API tokens, passwords, and sensitive user info.
await SecureStorage.SetAsync("token", "ABC123");
string token = await SecureStorage.GetAsync("token");
β Automatically encrypted β Keychain (iOS/macOS), Keystore (Android)
SQLite is the best option for structured or relational data storage. MAUI uses SQLite-net library.
dotnet add package sqlite-net-pcl
public class Note
{
[PrimaryKey, AutoIncrement]
public int Id { get; set; }
public string Text { get; set; }
public DateTime Created { get; set; }
}
public class NotesDatabase
{
private readonly SQLiteAsyncConnection db;
public NotesDatabase(string path)
{
db = new SQLiteAsyncConnection(path);
db.CreateTableAsync<Note>().Wait();
}
public Task<List<Note>> GetNotesAsync()
{
return db.Table<Note>().ToListAsync();
}
public Task SaveNoteAsync(Note note)
{
return db.InsertAsync(note);
}
}
string dbPath = Path.Combine(FileSystem.AppDataDirectory, "notes.db3"); builder.Services.AddSingleton(new NotesDatabase(dbPath));
public partial class NotesViewModel : ObservableObject
{
private readonly NotesDatabase db;
public NotesViewModel(NotesDatabase service)
{
db = service;
}
[ObservableProperty]
private List<Note> notes;
[RelayCommand]
private async Task LoadNotes()
{
Notes = await db.GetNotesAsync();
}
}
β SQLite is ideal for offline apps.
MAUI gives access to device folders like AppDataDirectory.
string path = Path.Combine(FileSystem.AppDataDirectory, "data.txt"); File.WriteAllText(path, "Hello world");
string content = File.ReadAllText(path);
await File.WriteAllTextAsync(path, "Async content"); string text = await File.ReadAllTextAsync(path);
β Good for logs, exports, user-generated text.
public partial class SettingsViewModel : ObservableObject
{
[ObservableProperty]
private string username;
[ObservableProperty]
private string secureToken;
[ObservableProperty]
private string fileContent;
private readonly NotesDatabase db;
public SettingsViewModel(NotesDatabase database)
{
db = database;
}
[RelayCommand]
private void SavePreferences()
{
Preferences.Set("username", Username);
}
[RelayCommand]
private async Task SaveSecure()
{
await SecureStorage.SetAsync("token", SecureToken);
}
[RelayCommand]
private async Task SaveFile()
{
string path = Path.Combine(FileSystem.AppDataDirectory, "data.txt");
await File.WriteAllTextAsync(path, FileContent);
}
[RelayCommand]
private async Task SaveNote()
{
await db.SaveNoteAsync(new Note
{
Text = FileContent,
Created = DateTime.Now
});
}
}
| MAUI-CommunityToolkit-7 :👈 | 👉:MAUI-CommunityToolkit-9 |