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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With