【发布时间】:2014-06-21 17:41:56
【问题描述】:
我有一个自定义类,它可以从独立存储中读取和写入。除图像外,我的所有值都已正确保存和检索。这是我的设置
设置.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);
这是我使用 PhotoChooserTask 收集图像并将结果保存到 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;
}
}
在同一个应用程序实例中,我可以将 MainPage 背景设置为 Settings.TransparentBackground.Value,效果很好,尽管当我完全重新启动应用程序时,Settings.TransparentBackground.Value 会返回 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;
关闭后应用程序中的任何地方都没有我将Settings.TransparentBackground.Value 重置为空。我不明白为什么只有这个值没有保存在 IsolatedStorage 中。
【问题讨论】:
标签: c# windows-phone-8 bitmap isolatedstorage bitmapimage