Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/silverlight/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用自定义类将列表或数组保存到applicationsettings_C#_Silverlight_Windows Phone 7_Application Settings - Fatal编程技术网

C# 使用自定义类将列表或数组保存到applicationsettings

C# 使用自定义类将列表或数组保存到applicationsettings,c#,silverlight,windows-phone-7,application-settings,C#,Silverlight,Windows Phone 7,Application Settings,我目前正在使用Adam nathan的《101 Windows Phone应用程序》一书中的应用程序设置类: public class Setting<T> { string name; T value; T defaultValue; bool hasValue; public Setting(string name, T defaultValue) { this.name = name; t

我目前正在使用Adam nathan的《101 Windows Phone应用程序》一书中的应用程序设置类:

    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
        {
            //checked for cached value
            if (!this.hasValue)
            {
                //try to get value from isolated storage
                if (!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name, out this.value))
                {
                    //not set yet
                    this.value = this.defaultValue;
                    IsolatedStorageSettings.ApplicationSettings[this.name] = this.value;
                }

                this.hasValue = true;
            }

            return this.value;
        }

        set
        {
            //save 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;
    }
}
公共类设置
{
字符串名;
T值;
T缺省值;
布尔值;
公共设置(字符串名称,T默认值)
{
this.name=名称;
this.defaultValue=defaultValue;
}
公共价值
{
得到
{
//已检查缓存值
如果(!this.hasValue)
{
//尝试从独立存储中获取价值
如果(!IsolatedStorageSettings.ApplicationSettings.TryGetValue(this.name,out this.value))
{
//还没定
this.value=this.defaultValue;
IsolatedStorageSettings.ApplicationSettings[this.name]=this.value;
}
this.hasValue=true;
}
返回此.value;
}
设置
{
//将价值保存到独立存储
IsolatedStorageSettings.ApplicationSettings[this.name]=值;
这个值=值;
this.hasValue=true;
}
}
公共T默认值
{
获取{返回this.defaultValue;}
}
//清除缓存值;
公共文件
{
this.hasValue=false;
}
}
然后在一个单独的课堂上:

公共静态类设置 { //设置菜单中的用户设置

    public static readonly Setting<bool> IsAerialMapStyle = new Setting<bool>("IsAerialMapStyle", false);
公共静态只读设置IsArialMapStyle=新设置(“IsArialMapStyle”,false);
}

这一切都很好,但我无法解决如何使用此方法将长度为24的数组或列表保存到应用程序设置

到目前为止,我有:

  public static readonly Setting<List<bool>> ListOpened = new Setting<List<bool>>("ListOpened",....
公共静态只读设置ListOpen=新设置(“ListOpen”,。。。。

非常感谢您的帮助!

不幸的是,您不能将其作为单个
键值
字典条目存储在
应用程序设置中。您只能存储内置数据类型(int、long、bool、string..)。要保存这样的列表,必须将对象序列化到内存中,或使用SQLCE数据库存储值(Mango)。

不幸的是,AFAIK不能将其作为单个
键值
字典条目存储在
应用程序设置中。只能存储内置数据类型(int、long、bool、string..)。要保存这样的列表,必须将对象序列化到内存中,或使用SQLCE数据库存储值(Mango)。

请参阅。 您需要通过类定义上的[DataContract]属性和每个字段(要保留)上的[DataMember]将设置类型声明为可序列化。哦,您还需要System.Runtime.Serialization

如果不想公开私有字段值(这些值被序列化为XML,并且可能被不适当地公开),则可以修饰属性声明,例如

using System.Runtime.Serialization;
. . .
[DataContract]
public class Settings {
    string Name;
    . . .
    [DataMember]
    public T Value {
    . . .
    }
如果您的类没有更新所有实例数据的属性,您可能还必须修饰这些私有字段。不必修饰公共属性和相应的私有字段

哦,您包装这个类的所有类型T也必须是可序列化的。基本类型是,但用户定义的类(可能还有一些CLR类型?)不是。

请参阅。 您需要通过类定义上的[DataContract]属性和每个字段(要保留)上的[DataMember]将设置类型声明为可序列化。哦,您还需要System.Runtime.Serialization

如果不想公开私有字段值(这些值被序列化为XML,并且可能被不适当地公开),则可以修饰属性声明,例如

using System.Runtime.Serialization;
. . .
[DataContract]
public class Settings {
    string Name;
    . . .
    [DataMember]
    public T Value {
    . . .
    }
如果您的类没有更新所有实例数据的属性,您可能还必须修饰这些私有字段。不必修饰公共属性和相应的私有字段

哦,您包装这个类的所有类型T也必须是可序列化的。基本类型是,但用户定义的类(可能还有一些CLR类型?)不是