3

This is actually a follow up to Storing data on Windows phone.

I have an app that loads a favourites page. If the favourites page is empty, the user can click a search button on the favourites page which loads a search page. There they can search for information and save it as a favourite. My code for saving the favourites is as follows.

stopNumber = txtBusStopNumber.Text;
IsolatedStorageSettings favouriteStops = IsolatedStorageSettings.ApplicationSettings;

if (!favouriteStops.Contains(stopNumber))
{
   favouriteStops.Add(stopNumber, stopAddress);
    favouriteStops.Save();
    MessageBox.Show("Favourite saved!");
}

The way the app works, once the user has added their favourites, they will navigate back to the previous page (the favourites page). When they navigate back to the favourites page, it needs to load the information that has just been added to IsolatedStorage via the Search page.

I know the code needs to be in the OnNavigatedTo event of the favourites page, but the issue is, my code doesn't seem to be reading any data.

protected override void OnNavigatedTo(NavigationEventArgs e)
{
    if (IsolatedStorageSettings.ApplicationSettings.Contains("stopNumber"))
    {
        for (int i = 0; i < IsolatedStorageSettings.ApplicationSettings.Count; i++)
        {
            lstStops.Items.Add(IsolatedStorageSettings.ApplicationSettings["stopNumber"] as string);
        }

    }

    base.OnNavigatedTo(e);
}

Is this because on the favourites page, some other instance of IsolatedStorage is being declared which doesn't contain any data? How do I access the data that was saved to IsolatedStorage on the search page, and iterate through it to find all the information?

Community
  • 1
  • 1
irldev
  • 409
  • 2
  • 8
  • 21
  • Why are you checking if it contains "stopNumber" in the second code segment? That is not something you need to do. It seems more like you want to do a foreach loop on the settings dictionary instead of a for loop as well. – steveg89 Jan 16 '14 at 20:29
  • As the first page that loads is the favourites page and I don't want it to try and iterate if the user has not added any favourites as there will be nothing to iterate over. – irldev Jan 16 '14 at 20:34
  • If there was nothing, the count would be 0 and the loop wouldn't execute anyways. – steveg89 Jan 16 '14 at 20:35

3 Answers3

2

Try something like

protected override void OnNavigatedTo(NavigationEventArgs e)
{
        foreach( KeyValuePair<string, Object> entry in IsolatedStorageSettings.ApplicationSettings)
        {
            lstStops.Items.Add(entry.Value as String);
        }

    base.OnNavigatedTo(e);
}

It iterates over the settings you stored and loads the value of each one into your list. You could also get the key (stopNumberin your code) if you wanted to by using entry.Key.

steveg89
  • 1,807
  • 2
  • 15
  • 29
  • I'm getting this error now: Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'System.Collections.Generic.KeyValuePair' – irldev Jan 16 '14 at 20:40
1

In your loop you tried each time to read "stopNumber" insted of all elements (what I soppose you wanted to do). Maybe try to do it like this:

protected override void OnNavigatedTo(NavigationEventArgs e)  
{
    for (int i = 0; i < IsolatedStorageSettings.ApplicationSettings.Count; i++)
    {
        lstStops.Items.Add(IsolatedStorageSettings.ApplicationSettings.ElementAt(i).Value as string);
    }

  base.OnNavigatedTo(e);
}

The code above is bad because of one point - in IsolatedStorageSettings you may save more things - not only your stopNumbers - and that loop will also use these variables - which can throw an exception.

On the other hand I would suggest to use IsolatedStorageFile instead of IsolatedStorageSettings. Create a class with your data and serialize it.

Community
  • 1
  • 1
Romasz
  • 29,662
  • 13
  • 79
  • 154
  • Unfortunately that gives this error: "Error 1 Cannot convert type 'System.Collections.Generic.KeyValuePair' to 'string' via a reference conversion, boxing conversion, unboxing conversion, wrapping conversion, or null type conversion" – irldev Jan 16 '14 at 20:37
  • @irldev what type is your stopAdress? – Romasz Jan 16 '14 at 20:40
  • stopAddress is a string. – irldev Jan 16 '14 at 20:42
0

I cannot yet comment on answers due to my reputation, but please be wary using steveg89's solution. If at any point you add more values to IsolatedStorageSettings that are unrelated to your 'favorites' those will also be iterated on when using his method.

I would instead suggest storing all 'favorites' in a Dictionary and then saving the IsolatedStorageSettings. When you'd like to add a new entry, simple load the Dictionary from settings, check if the value exists in the Dictionary, and if not add it and then resave to IsolatedStorageSettings.

Then, in order to load the values, simply load the Dictionary and iterate through that instead.

Something like this:

//Loading items into Dictionary

Dictionary<string, string> tempDictionary = new Dictionary<string, string>();

if (IsolatedStorageSettings.ApplicationSettings.Contains("stopNumberDictionary"))
{
    tempDictionary = IsolatedStorageSettings.ApplicationSettings["stopNumberDictionary"] as Dictionary<string, string>;
}
else
{
    IsolatedStorageSettings.ApplicationSettings.Add("stopNumberDictionary", tempDictionary);
}

if (!tempDictionary.Contains(stopNumber))
{
    tempDictionary.Add(stopNumber, stopAddress);
}

IsolatedStorageSettings.ApplicationSettings["stopNumberDictionary"] = tempDictionary;

IsolatedStorageSettings.ApplicationSettings.Save();


//Then load Dictionary OnNavigatedTo

protected override void OnNavigatedTo(NavigationEventArgs e)  
{
    Dictionary<string, string> tempDictionary = new Dictionary<string, string>();

    if (IsolatedStorageSettings.ApplicationSettings.Contains("stopNumberDictionary"))
    {
        tempDictionary = IsolatedStorageSettings.ApplicationSettings["stopNumber"] as Dictionary<string, string>;
    }

    foreach(KeyValuePair<string, string> entry in tempDictionary)
    {
        lstStops.Items.Add(entry.Value as string);
    }

    base.OnNavigatedTo(e);
}
philorube
  • 155
  • 8
  • That's true but there's not really a great way to do it otherwise unless he implements a common pattern that you could use in a LINQ command to search out the wanted items. – steveg89 Jan 16 '14 at 21:04
  • Thanks for the feedback. At the moment I just want to store favourites in IsolatedStorage Settings. I noticed this problem earlier when storing some other information in IsolatedStorage settings also, it got iterated and added to the favourites. For the moment, that extra information I had there is not needed but I will keep this in mind. – irldev Jan 16 '14 at 21:17
  • You're welcome. I added some code to my answer that should make things a bit more organized. – philorube Jan 16 '14 at 21:18