I have been using a helper class that saves and retrieves values from isolated storage using key, value pairs. This has worked for individual values that are changed in my application, but I am now attempting to use it for an ObservableCollection in which items will be added and removed during runtime. For some reason, my ObservableCollection is not stored and/or retrieved from IsolatedStorage when the application is closed and reopened. I am not sure what I am doing wrong, because this works for individual values just fine?
Setting.cs
public class Setting<T>
{
string name;
T value;
T defaultValue;
bool hasValue;
public Setting(string name, T defaultValue)
{
this.name = name;
this.defaultValue = defaultValue;
}
public T Value
{
get
{
//Check for the cached value
if (!this.hasValue)
{
//Try to get the value from Isolated Storage
if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))
{
//It hasn't been set yet
this.value = this.defaultValue;
IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
}
this.hasValue = true;
}
return this.value;
}
set
{
//Save the value to Isolated Storage
IsolatedStorageSettings.ApplicationSettings[this.name] = value;
this.value = value;
this.hasValue = true;
}
}
public T DefaultValue
{
get { return this.defaultValue; }
}
// Clear cached value
public void ForceRefresh()
{
this.hasValue = false;
}
}
The above Setting.cs class is used to store and retrieve values, and I am using another class called Settings.cs as a mechanism for using those stored values.
Settings.cs
public static Setting<ObservableCollection<BrowserItem>> BrowserList = new Setting<ObservableCollection<BrowserItem>>("Browsers", new ObservableCollection<BrowserItem>());
public static Setting<string> InitialUri = new Setting<string>("InitialUri", "http://www.bing.com");
In the above, InitialUri
may be changed and the value is saved and retrieved correctly. On the other hand, BrowserList
(the ObservableCollection of type BrowserItem (a custom class)) is not stored or saved? The following shows how I am attempting to use BrowserList
BrowserItem.cs
[DataContract]
public class BrowserItem
{
[DataMember]
public FullWebBrowser Browser
{
get;
set;
}
[DataMember]
public string Url
{
get;
set;
}
[DataMember]
public BitmapImage ImageUri
{
get;
set;
}
[DataMember]
public string Title
{
get;
set;
}
[DataMember]
public string Notification
{
get;
set;
}
[DataMember]
public bool DisplayNotification
{
get
{
return !string.IsNullOrEmpty(this.Notification);
}
}
[DataMember]
public string Message
{
get;
set;
}
[DataMember]
public string GroupTag
{
get;
set;
}
[DataMember]
//for translation purposes (bound to HubTile Title on MainPage)
public string TileName
{
get;
set;
}
}
TabsPage.xaml.cs
void addNew_Click(object sender, EventArgs e)
{
BitmapImage newTileImage = new BitmapImage();
var newItem = new BrowserItem() { Browser = new FullWebBrowser(), Url = "http://www.bing.com", ImageUri = newTileImage, Title = "new", /*Notification = "",*/ Message = "new browser", GroupTag = "TileGroup", TileName = "new" };
newItem.Browser.InitialUri = Settings.InitialUri.Value; //set the initial uri when created
Settings.BrowserList.Value.Add(newItem); //update saved BrowserList
}
In the addNew_Click
even handler, the new BrowserItem
named newItem
is successfully added. However when the app closes or reopens, I check the BrowserList
to see if items exist, and if they do I would like to load a specific one based on the items index value in the ObservableCollection. Everytime I perform the check, BrowserList
has no saved items? How can I properly save these values in the collection so that they will persist?