Search code examples
c#xamluwp

What is the best way to store collections in the local folder?


I've a simple app with a simple observable collection. User can manually add item to list by clicking add button and can give it a name and some other related properties.

I am trying to store those properties, for example let's say user add a new item with name "Sample" now when the app is terminated or user navigates to another page. The collection is reset with no items. So what is the best way to store value of each item and then restore it back when app loads or navigates to that page.

Note: User is given the right to add as many items as he wants and each items has total of eight(can vary) different properties.


Solution

  • You have 2 issues here.

    maintaining ObservableCollection when the user navigates to different view from current view.

    To handle this, you can initialize your ObservableCollection globally once instead of in the page where you use it. Likely in App.xaml.cs

    So if you have a ObservableCollection<string> that you use, Add this initialization in App.xaml.cs as

    public ObservableCollection<string> GlobalList {get; set;} = new ObservableCollectin<string>;
    

    Now in any view you can use App reference as like App.GlobalList to do Add,Remove and what other functions you want to use.

    maintaining ObservableCollection when the user closes the app.

    I strongly recommend Newtonsoft.Json. Simple, easy, fast and lightweight Json Parser to convert your ObservableCollection to Json and back.

    You can convert Your ObservableCollection to json string like below.

    string jsonData = JsonConvert.SerializeObject(App.GlobalList);
    

    You can convert your json to ObservableCollection like below.

    App.GlobalList = JsonConvert.DeserializeObject<ObservableCollection<string>>(jsonData);
    

    you can save and retrieve the data using the methods below. (GIST)

    public async void SaveList(string FileName, string StringValue)
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
    
        StorageFile sampleFile = (File.Exists(Path.Combine(storageFolder.Path,FileName))) ?
            await storageFolder.GetFileAsync(FileName)
           :await storageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
    
        await FileIO.WriteTextAsync(sampleFile, StringValue);
    }
    
    public async Task<string> RetrieveList(string FileName)
    {
        StorageFolder storageFolder = ApplicationData.Current.LocalFolder;
        StorageFile sampleFile = (File.Exists(Path.Combine(storageFolder.Path,FileName))) ?
                await storageFolder.GetFileAsync(FileName)
               : await storageFolder.CreateFileAsync(FileName, CreationCollisionOption.ReplaceExisting);
        return await FileIO.ReadTextAsync(sampleFile);
    }
    

    So to Save your json, you need to call

    SaveList("GlobalList.txt", jsonData);
    

    and to retrieve you need to call

    string jsonData = await RetrieveList("GlobalList.txt");
    

    Good Luck.