How can I make data in ObservableCollection / List persistent?

mrizoiwe98 66 Reputation points
2021-03-30T22:54:49.55+00:00

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>();           
       }
Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,291 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JessieZhang-MSFT 7,701 Reputation points Microsoft Vendor
    2021-03-31T02:45:07.357+00:00

    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://learn.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.

    0 comments No comments