【问题标题】:IsolatedStorage Not Saving Value隔离存储不保存价值
【发布时间】: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


    【解决方案1】:

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

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

    来源 - Quickstart: Working with settings for Windows Phone 8

    因此,您需要手动存储 BitmapImage。有关将图像存储到本地存储的许多其他旧问题,请参阅this one

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多