标签:style blog http color io os ar java strong
一、Application Data简介
Applicaion Data相当于桌面应用的注册表,存储一些用户配置信息,如运行时状态,用户喜好等,需要注意的时,当卸载应用时,这些数据会被删除,所以不要存储重要数据。
Applicaion Data即使在应用更新以后也不会丢失,有一个版本控制。
二、按用途分类
三、按存储方式分类
App settings
用ApplicationData.LocalSettings这个静态属性来取得ApplicationDataContainer类类型的配置。
App Files
用ApplicationData.LocalFolder这个静态属性来取StorageFolder类型的文件。
四、代码示例
存储本地数据
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;//存储单一配置的句柄
ApplicationDataCompositeValue composite = new ApplicationDataCompositeValue();//存储多项配置句柄
StorageFolder localFolder = ApplicationData.Current.LocalFolder;//存储文件句柄
localSettings.Values["exampleSetting"] = "Hello Windows";;//存储单一配置 composite["intVal"] = 1; composite["strVal"] = "string"; localSettings.Values["exampleCompositeSetting"] = composite;//存储多项配置
建立配置层级
ApplicationDataContainer container =
localSettings.CreateContainer("exampleContainer", ApplicationDataCreateDisposition.Always);
if (localSettings.Containers.ContainsKey("exampleContainer"))
{
localSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello Windows";
}
读取配置
Object value = localSettings.Values["exampleSetting"];//a setting
ApplicationDataCompositeValue composite =
(ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"];
if (composite == null)
{
// No data
}
else
{
// Access data in composite["intVal"] and composite["strVal"]
}
bool hasContainer = localSettings.Containers.ContainsKey("exampleContainer");
bool hasSetting = false;
if (hasContainer)
{
hasSetting = localSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting");
}
删除配置
localSettings.Values.Remove("exampleSetting");
//ApplicationDataContainerSettings.Remove
localSettings.Values.Remove("exampleCompositeSetting");
//ApplicationDataCompositeValue.Remove
localSettings.DeleteContainer("exampleContainer");
//ApplicationDataContainer.DeleteContainer
标签:style blog http color io os ar java strong
原文地址:http://www.cnblogs.com/fuchongjundream/p/4011649.html