I have a custom class that reads and writes from and to IsolatedStorage. All of my values are being saved and retrieved properly except an image. Here is my setup
Setting.cs
//Encapsulates a key/value pair stored in Isolated Storage ApplicationSettings
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;
}
}
Settings.cs
//Transparent Background
public static readonly Setting<BitmapImage> TransparentBackground = new Setting<BitmapImage>("TransparentBackground", null);
Here is where I gather the Image using PhotoChooserTask and save the result to IsolatedStorage
Settings.Page.xaml.cs
private void Browse_Click(object sender, RoutedEventArgs e)
{
photoChooserTask.Show();
}
void photoChooserTask_Completed(object sender, PhotoResult e)
{
if (e.TaskResult == TaskResult.OK)
{
//Code to display the photo on the page in an image control named TransparentModeViewBoxImage.
BitmapImage bmp = new BitmapImage();
bmp.SetSource(e.ChosenPhoto);
TransparentModeViewBoxImage.Source = Settings.TransparentBackground.Value = bmp;
}
}
In the same app instance, I can set the MainPage background to Settings.TransparentBackground.Value which works great, although when I completely restart the app Settings.TransparentBackground.Value returns to null.
MainPage.xaml.cs
ImageBrush ib = new ImageBrush();
if(Settings.TransparentBackground.Value == null)
//Use no background image
else
ib.ImageSource = Settings.TransparentBackground.Value;
LayoutRoot.Background = ib;
Nowhere in the app upon closing to I reset Settings.TransparentBackground.Value to null. I cannot figure out why only this value does not save in IsolatedStorage.