Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top