Créer une App Moderne — Avalonia UI & .NET
Exemple d'implémentation
public sealed class JsonTodoRepository : ITodoRepository
{
private readonly string _filePath =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
"TodoApp", "todos.json");
public async Task<IReadOnlyList<TodoItem>> GetAllAsync()
{
if (!File.Exists(_filePath)) return Array.Empty<TodoItem>();
var json = await File.ReadAllTextAsync(_filePath);
return JsonSerializer.Deserialize<List<TodoItem>>(json) ?? new();
}
public async Task AddAsync(TodoItem item)
{
var items = (await GetAllAsync()).ToList();
items.Add(item);
Directory.CreateDirectory(Path.GetDirectoryName(_filePath)!);
await File.WriteAllTextAsync(_filePath,
JsonSerializer.Serialize(items, new JsonSerializerOptions { WriteIndented = true }));
}
}
[{ "title": "Préparer la démo", "isDone": false }]