IsolatedStorage不保存值

本文关键字:保存 IsolatedStorage | 更新日期: 2023-09-27 18:09:47

我有一个自定义类,从IsolatedStorage读取和写入。我所有的值都被保存和检索正确,除了一个图像。这是我的设置

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);

这里是我使用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;
        }
    }    

在同一个应用程序实例中,我可以将主页背景设置为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为null。我不明白为什么只有这个值不保存在IsolatedStorage

IsolatedStorage不保存值

您正在尝试将其存储到isolatedstoragessettings。ApplicationSettings字典。通常,这用于较小的数据块,更重要的是-可以序列化的数据。

键值对由唯一的键标识符和关联的在哈希表中找到的数据值。isolatedstoragessettings是一个字典类,用于将数据保存或检索为键/值对。你可以存储任何序列化对象在这个字典与字符串关键。

来源-快速入门:使用Windows Phone 8的设置

因此,您需要手动存储BitmapImage。参考关于将图像存储到本地存储的许多其他老问题,例如: