0
votes

I am storing the observable collection in my application isolated storage settings. The list is getting saved successfully during the application exit. But when I launch the application again its not able to find the stored key from the isolated storage.

Here is my code

    void LoadSettings()
    {
        if (settings.Contains("DiaryItems"))
        {
            diaryItems = (ObservableCollection<MyDiaryItem>)settings["DiaryItems"];
        }
    }

    void SaveSettings()
    {
        settings["DiaryItems"] = diaryItems;
    }

I am calling SaveSettings method during my application closing and application deactivated.

I am calling LoadSettings method during my application launching and application activated.

When I was debugging I could see an error saying that

'settings["DiaryItems"]' threw an exception of type
'System.Collections.Generic.KeyNotFoundException'

Just wondering what could be wrong with the settings. The same code works for a simple list of type List.

3

3 Answers

2
votes

Actually when you want to store any items in the IsolatedStorage you have to call the save of ApplicationSettings function

so modify your save function as follows,

void SaveSettings()
    {
        settings["DiaryItems"] = diaryItems;
        settings.Save();
    }

Sorry i forgot to mention you that , your MyDiaryItem, should be properly serializable.

2
votes

Note sure why it doesn't work, but you can try to wrap your ObservableCollection inside of a list:

void LoadSettings()
{
    if (settings.Contains("DiaryItems"))
    {
        diaryItems = new ObservableCollection<MyDiaryItem>((List<MyDiaryItem>)settings["DiaryItems"]);
    }
}

void SaveSettings()
{
    settings["DiaryItems"] = diaryItems.ToList();
}

You need to have using System.Linq; at the top of your file, or the 'ToList' won't work.

0
votes

try to add your data in dictionary using Add function like

isolatedstorege.Add(key,value);

this way it adds the value more