Saving ObservableCollection to Isolated Storage

I am making a note taking application in which a user can create, edit and delete notes. After closing the application, all data should be stored in isolated storage. I created a class of notes that sets some properties below:

    public string strNoteName { get; set; }
    public string strCreated { get; set; }
    public string strModified { get; set; }
    public bool boolIsProtected { get; set; }
    public string strNoteImage { get; set; }
    public string strNoteSubject { get; set; }
    public string strTextContent { get; set; }

They are placed in ObservableCollection<note> GetnotesRecord(), which can be displayed on the main page using a list. When touched, there is an event handler for SelectionChangethat passes the element to an editable page, where elements such as strTextContent and strNoteName can be edited.

After adding all this, I want the data to be stored in isolated storage so that it can be loaded the next time the application is launched.

Is it possible to save ObservableCollection<note>? If so, how can it be extracted from isolated storage when the application starts later?

+4
source share
1 answer

Steps: -

If your collection is large, then convert the ObservealCollection to an xml string and save it using the class IsolatedStorageSettingsas a Key-Value pair.

If this is not the case: - then you can do IsolatedStorageSettings directly as follows

IsolatedStorageSettings Store { get { return IsolatedStorageSettings.ApplicationSettings; } }

    public T GetValue<T>(string key)
    {
        return (T)Store[key];
    }

    public void SetValue(string token, object value)
    {
        Store.Add(token, value);
        Store.Save();
    }

Usage: -

    ObservableCollection<Note> objCollection = new ObservableCollection<Note>()
    {
        new Note(){Checkbool = false,Checkme = "sd"},
        new Note(){Checkbool = false,Checkme = "sd1"},
        new Note(){Checkbool = false,Checkme = "sd2"}
    };

    // you can also make check whether values are present or 
    // by checking the key in storage.
    var isContainKey = Store.Contains("set")

    // save key value pair
    SetValue("set", objCollection); 

    // extract key value pair
    var value = GetValue<ObservableCollection<Note>>("set"); 
+3
source

All Articles