.NET MAUI for Beginners course on Youtube by James Montemagno

Micha Jovanovic 20 Reputation points
2023-09-04T11:17:41.4266667+00:00

There is a good course in Net Maui for Beginners: https://www.youtube.com/watch?v=5Qga2pniN78&list=PLdo4fOcmZ0oUBAdL2NwBpDs32zwGqb9DY&index=6

But: after installing the app on my android smartphone, the entries disappear after a day or so. It is a simple "list to do" app in .Net Maui. How to save user data in .Net Maui?

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,984 questions
.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
3,722 questions
0 comments No comments
{count} votes

Accepted answer
  1. Wenyan Zhang (Shanghai Wicresoft Co,.Ltd.) 33,176 Reputation points Microsoft Vendor
    2023-09-05T07:25:12.6366667+00:00

    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.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.