使用自定义类将列表或数组保存到应用程序设置
我目前正在使用 admam nathan 的书 101 Windows Phone Apps 中找到的应用程序设置类:
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;
}
}
然后在一个单独的类中:
公共静态类设置 { //设置菜单中的用户设置
public static readonly Setting<bool> IsAerialMapStyle = new Setting<bool>("IsAerialMapStyle", false);
}
一切正常,但我无法弄清楚如何使用此方法将长度为 24 的数组或列表保存到应用程序设置中。
到目前为止我有这个:
public static readonly Setting<List<bool>> ListOpened = new Setting<List<bool>>("ListOpened",....
任何帮助将不胜感激!
I am currently using the application settings class found in admam nathan's book 101 Windows Phone Apps:
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;
}
}
and then in a seperate class:
public static class Settings
{
//user settings from the settings menu
public static readonly Setting<bool> IsAerialMapStyle = new Setting<bool>("IsAerialMapStyle", false);
}
It all works fine but I can't work out how to save an array or list of length 24 to the application settings using this method.
I have this so far:
public static readonly Setting<List<bool>> ListOpened = new Setting<List<bool>>("ListOpened",....
Any help would be much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅使用数据协定。
您需要通过类定义上的 [DataContract] 属性和每个字段(您想要保留的)上的 [DataMember] 声明您的设置类型可序列化。哦,你需要 System.Runtime.Serialization。
如果您不想公开私有字段值(这些值被序列化为 XML 并且可能会不适当地公开),您可以修饰属性声明,例如,
如果您的类没有更新所有实例数据的属性,您可能会还必须装饰那些私人领域。没有必要同时装饰公共财产和相应的私人领域。
哦,对这个类进行包装的所有类型 T 也必须是可序列化的。原始类型是,但用户定义的类(也许还有一些 CLR 类型?)则不是。
See Using Data Contracts.
You need to declare your Setting type to be serializable via [DataContract] attribute on the class definition and [DataMember] on each field (that you want to preserve). Oh, and you need System.Runtime.Serialization.
If you do not want to expose the private field values (the values are serialized to XML and might be exposed inappropriately), you could decorate the property declarations, e.g.
If your class doesn't have properties that update all the instance data, you might also have to decorate those private fields as well. It's not necessary to decorate both the public property and the corresponding private field.
Oh, and all the types T that you wrap this class around have to be serializable, too. The primitive types are, but user-defined classes (and maybe some CLR types?) would not be.
不幸的是,据我所知,您无法将其作为单个
键值
字典条目存储在ApplicationSettings
中。您只能存储内置数据类型(int、long、bool、string...)。要保存这样的列表,您必须将对象序列化到内存中,或者使用 SQLCE 数据库来存储值 (Mango)。Unfortunately, AFAIK, you cannot store it in
ApplicationSettings
as a singlekey value
dictionary entry. You can only store built-in data types(int, long, bool, string..). To save a list like this, you'll have to serialize the object into memory, or use the SQLCE database to store the values (Mango).