question

mrizoiwe98 avatar image
0 Votes"
mrizoiwe98 asked JessieZhang-2116 edited

How can I make data in ObservableCollection / List persistent?

I am using a simple Observable Collection to store list entries in my notepad app. How can I make entries in this collection persistent?

  public ObservableCollection<string> ListItem { get; set; }
        public string Items { get; set; }
          public MainPageViewModel()
        {
            ListItem = new ObservableCollection<string>();           
        }
dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JessieZhang-2116 avatar image
0 Votes"
JessieZhang-2116 answered JessieZhang-2116 edited

Hello,


Welcome to our Microsoft Q&A platform!

If you want to keep the value of ListItem utill you close your app , you can add a keyword static for ListItem .

  public static ObservableCollection<string> ListItem { get; set; }

If you want to keep the value even after your close your app, you can use local data storage to store your data.

1.Files
File handling with Xamarin.Forms can be achieved using code in a .NET Standard library, or by using embedded resources. This article explains how to perform file handling from shared code in a Xamarin.Forms application.

2.Local Databases
Xamarin.Forms supports database-driven applications using the SQLite database engine, which makes it possible to load and save objects in shared code. This article describes how Xamarin.Forms applications can read and write data to a local SQLite database using SQLite.Net.

For more details, you can check: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/data-cloud/data/

Best Regards,

Jessie Zhang


If the response is helpful, please click "Accept Answer" and upvote it.


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.





5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.