C# IsolatedStorage未保存值

C# IsolatedStorage未保存值,c#,windows-phone-8,bitmap,isolatedstorage,bitmapimage,C#,Windows Phone 8,Bitmap,Isolatedstorage,Bitmapimage,我有一个自定义类,可以从和写入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;

我有一个自定义类,可以从和写入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.TransparentBackground.Value
,这非常有效,尽管当我完全重新启动应用程序
Settings.TransparentBackground.Value
时返回空值

MainPage.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;
        }
    }    
ImageBrush ib = new ImageBrush();

        if(Settings.TransparentBackground.Value == null)
            //Use no background image
        else
            ib.ImageSource = Settings.TransparentBackground.Value;

        LayoutRoot.Background = ib;

关闭应用程序后,应用程序中没有任何位置,我将设置重置为空。我无法理解为什么只有此值不保存在IsolatedStorage中。

您正在尝试将其存储到IsolatedStorage Settings.ApplicationSettings字典中。通常,这用于较小的数据段,更重要的是,用于可序列化的数据

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

来源-

因此,您需要手动存储BitmapImage。请参阅有关将映像存储到本地存储器的许多其他老问题,例如

ImageBrush ib = new ImageBrush();

        if(Settings.TransparentBackground.Value == null)
            //Use no background image
        else
            ib.ImageSource = Settings.TransparentBackground.Value;

        LayoutRoot.Background = ib;