Hello,
the entries disappear after a day or so
When you kill the app then re-open it, the data has been removed, because all items are saved in a ObservableCollection<T>
.
How to save user data in .Net Maui?
There are three ways to deal with the date in the collection.
You could try to serializer the collection, then save all items in local storage.
For more details, please refer to How to serialize and deserialize JSON in C# - .NET | Microsoft Learn
Each operating system will have unique paths to the app cache and app data directories. See File system helpers - .NET MAUI | Microsoft Learn
The following code is to save the Monkeys
in the official CollectionView sample:
private void SaveButton_Clicked(object sender, EventArgs e)
{
MonkeysViewModel vm = this.BindingContext as MonkeysViewModel;
string jsonString = JsonSerializer.Serialize(vm.Monkeys);
var path = Path.Combine(FileSystem.Current.AppDataDirectory, "Collection.txt");
File.WriteAllText(path, jsonString);
}
private void ReadButton_Clicked_1(object sender, EventArgs e)
{
var path = Path.Combine(FileSystem.Current.AppDataDirectory, "Collection.txt");
string jsonString = File.ReadAllText(path);
ObservableCollection<Monkey> desMonkeys = JsonSerializer.Deserialize<ObservableCollection<Monkey>>(jsonString);
}
You could also try to integrate SQLite.NET, then store and retrieve information in a local database.
Please see .NET MAUI local databases - .NET MAUI | Microsoft Learn
In addition, you could try WebService to send data to servers and retrieve data from the web service.
For more details, please refer to Consume a REST-based web service - .NET MAUI | Microsoft Learn and Connect to local web services from Android emulators and iOS simulators - .NET MAUI | Microsoft Learn
Best Regards,
Wenyan Zhang
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.