了解如何存储和检索本地应用数据存储中的设置和文件。

路线图: 本主题与其他主题有何关联?请参阅:

获取应用的设置和文件容器

使用 ApplicationData.LocalSettings 属性可以获取 ApplicationDataContainer 对象中的设置。使用ApplicationData.LocalFolder 属性可以获取 StorageFolder 对象中的文件。

 
Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder;

后面的部分使用此部分中的 localSettings 和 localFolder 变量。

将数据写入设置

使用 ApplicationDataContainer.Values 属性可以访问我们在上一步中获取的 localSettings 容器中的设置。此示例会创建一个名为 exampleSetting 的设置。

// Simple setting

localSettings.Values["exampleSetting"] = "Hello Windows";

ApplicationDataCompositeValue 对象包含必须以原子方式访问的设置。此示例会创建一个名为exampleCompositeSetting 的复合设置并将它添加到 localSettings 容器中。

 
// Composite setting

Windows.Storage.ApplicationDataCompositeValue composite = new Windows.Storage.ApplicationDataCompositeValue();
composite["intVal"] = 1;
composite["strVal"] = "string"; localSettings.Values["exampleCompositeSetting"] = composite;

调用 ApplicationDataContainer.CreateContainer 方法可创建设置容器。此示例创建一个名为exampleContainer 的设置容器并添加一个名为 exampleSetting 的设置。ApplicationDataCreateDisposition枚举中的 Always 值指示创建容器(如果尚不存在的话)。

// Setting in a container

Windows.Storage.ApplicationDataContainer container =
localSettings.CreateContainer("exampleContainer", Windows.Storage.ApplicationDataCreateDisposition.Always); if (localSettings.Containers.ContainsKey("exampleContainer"))
{
localSettings.Containers["exampleContainer"].Values["exampleSetting"] = "Hello Windows";
}

从设置中读取数据

使用 ApplicationDataContainer.Values 属性可以访问 localSettings 容器中的 exampleSetting 设置。

// Simple setting

Object value = localSettings.Values["exampleSetting"];

使用 ApplicationDataContainer.Values 属性可以访问 localSettings 容器中的 exampleCompositeSetting 设置。

// Composite setting

Windows.Storage.ApplicationDataCompositeValue composite =
(Windows.Storage.ApplicationDataCompositeValue)localSettings.Values["exampleCompositeSetting"]; if (composite == null)
{
// No data
}
else
{
// Access data in composite["intVal"] and composite["strVal"]
}

使用 ApplicationDataContainer.Values 属性可以访问 exampleContainer 容器中的 exampleSetting 设置。

// Setting in a container

bool hasContainer = localSettings.Containers.ContainsKey("exampleContainer");
bool hasSetting = false; if (hasContainer)
{
hasSetting = localSettings.Containers["exampleContainer"].Values.ContainsKey("exampleSetting");
}

将数据写入文件

使用文件 API(如 Windows.Storage.StorageFolder.CreateFileAsync 和Windows.Storage.FileIO.WriteTextAsync)在本地应用数据存储中创建和更新文件。此示例会在 localFolder容器中创建一个名为 dataFile.txt 的文件并将当前日期和时间写入该文件中。CreationCollisionOption 枚举中的ReplaceExisting 值指示替换该文件(如果存在的话)。

在 Windows Phone 设备上,默认情况下会备份应用数据。如果你不想要备份某个文件,请将其保存在应用的本地存储的 LocalCache 子文件夹中。

 
async void WriteTimestamp()
{
Windows.Globalization.DateTimeFormatting.DateTimeFormatter formatter =
new Windows.Globalization.DatetimeFormatting.DateTimeFormatter("longtime"); StorageFile sampleFile = await localFolder.CreateFileAsync("dataFile.txt",
CreateCollisionOption.ReplaceExisting);
await FileIO.WriteTextAsync(sampleFile, formatter.Format(DateTime.Now));
}

从文件中读取数据

使用文件 API(如Windows.Storage.StorageFolder.GetFileAsyncWindows.Storage.StorageFile.GetFileFromApplicationUriAsync 和 Windows.Storage.FileIO.ReadTextAsync)在本地应用数据存储中打开和读取文件。此示例打开在上一步中创建的 dataFile.txt 文件并从该文件中读取日期。有关从多个位置加载文件资源的详细信息,请参阅如何加载文件资源

async void ReadTimestamp()
{
try
{
StorageFile sampleFile = await localFolder.GetFileAsync("dataFile.txt");
String timestamp = await FileIO.ReadTextAsync(sampleFile);
// Data is contained in timestamp
}
catch (Exception)
{
// Timestamp not found
}
}

删除已完成使用的设置

调用 ApplicationDataContainerSettings.Remove 方法可在完成对 exampleSetting 设置的使用之后将其删除。

// Delete simple setting

localSettings.Values.Remove("exampleSetting");

调用 ApplicationDataCompositeValue.Remove 方法可在完成对 exampleCompositeSetting 复合设置的使用之后将其删除。

// Delete composite setting

localSettings.Values.Remove("exampleCompositeSetting");

调用 ApplicationDataContainer.DeleteContainer 方法可在完成对 exampleContainer 设置容器的使用之后将其删除。

 
// Delete container

localSettings.DeleteContainer("exampleContainer");

WP8.1学习系列(第二十一章)——本地应用数据的更多相关文章

  1. WP8.1学习系列(第二十七章)——ListView和GridView入门

    快速入门:添加 ListView 和 GridView 控件 (XAML)   在本文中 先决条件 选择 ListView 或 GridView 将项添加到项集合 设置项目源 指定项目的外观 指定视图 ...

  2. WP8.1学习系列(第二十三章)——到控件的数据绑定

    在本文中 先决条件 将控件绑定到单个项目 将控件绑定到对象的集合 通过使用数据模板显示控件中的项目 添加详细信息视图 转换数据以在控件中显示 相关主题 本主题介绍了如何在使用 C++.C# 或 Vis ...

  3. WP8.1学习系列(第十一章)——中心控件Hub开发指南

    在本文中 先决条件 什么是中心控件? 添加中心控件 将分区添加到中心 添加交互式分区头用于导航 将展示磁贴添加到中心 使用窄应用中的垂直中心 借助中心使用语义式缩放视图 摘要和后续步骤 重要的 API ...

  4. WP8.1学习系列(第二十章)——添加控件和处理事件

    先决条件 添加控件 设置控件的名称 设置控件属性 创建事件处理程序 新控件 总结 相关主题 通过使用如按钮.文本框和组合框等控件,你可以创建应用的 UI. 下面将显示如何将控件添加到应用.处理控件时, ...

  5. WP8.1学习系列(第二十二章)——在页面之间导航

    在本文中 先决条件 创建导航应用 Frame 和 Page 类 页面模板中的导航支持 在页面之间传递信息 缓存页面 摘要 后续步骤 相关主题 重要的 API Page Frame Navigation ...

  6. WP8.1学习系列(第二十五章)——控件样式

      XAML 框架提供许多自定义应用外观的方法.通过样式可以设置控件属性,并重复使用这些设置,以便保持多个控件具有一致的外观. 路线图: 本主题与其他主题有何关联?请参阅: 使用 C# 或 Visua ...

  7. WP8.1学习系列(第二十六章)——控件模板

    在本文中 自定义控件模板示例 指定控件的可视结构. 指定控件的可视行为 使用工具轻松处理主题 控件和辅助功能 了解有关控件默认模板的详细信息 控件模板中的主题资源 相关主题 在 XAML 框架中,如果 ...

  8. WP8.1学习系列(第二十四章)——Json解析

    .net已经集成了json解析,类名叫DataContractJsonSerializer DataContractJsonSerializer 类型公开以下成员. 构造函数     名称 说明 Da ...

  9. WP8.1学习系列(第二章)——Toast通知

    Toast 通知概述(Windows 运行时应用) 你的应用要想通过 Toast 通知通信,必须在应用的清单文件中声明它支持 Toast.Toast 通知可包含文本,并且 Windows 上的 Toa ...

随机推荐

  1. Ubuntu -- 安装、卸载程序的命令

    通过sudo apt-get install xxxx 安装软件后,总是无法卸载干净,这里以Apache 为例,提供方法:首先sudo apt-get remove apache2再sudo apt- ...

  2. Python——eventlet.greenthread

    该模块实现 eventlet 中的 “绿色线程” 即协程. 相关的 greenlet 模块的介绍. 目录 一.模块级函数 sleep() spawn() 模块级函数 eventlet.greenthr ...

  3. C# Image与Base64编码互转函数

    public Bitmap GetImageFromBase64(string base64string) { byte[] b = Convert.FromBase64String(base64st ...

  4. iOS:PSTCollectionView

    https://github.com/steipete/PSTCollectionView Open Source, 100% API compatible replacement of UIColl ...

  5. iOS : Blur Effect

    http://blog.bubbly.net/2013/09/11/slick-tricks-for-ios-blur-effect/ Full sample sources available at ...

  6. .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录?

    .net framework 4.0 在 VS2010 安装目录下位置 dotNetFx40_Full_x86_x64.exe在磁盘哪个目录? 使用VS2010开发应用程序完毕后,在发布应用程序时,常 ...

  7. C# WORD操作实现代码(转载)

    在当前项目开发过程中,客户有根据数据库数据生成WORD文档的需求,在和同事沟通的过程中,找到了两个解决方案 1.先通过程序生成报表样式的HTML页面,然后修改HTML页面的后缀名为DOC. 2.定制W ...

  8. vue实现文章内容过长点击阅读全文功能

    直接上代码: html: <div class="bodyFont clearfloat" id="bodyFont" ref="bodyFon ...

  9. QT编译错误:undefined reference to `__imp_gl*'等等

    学习QT OpenGL绘制图形,程序中使用了OpenGL的API函数(gl开头),但是编译出现了错误:截图如下 有过编程经验的人可知,是链接的时候出错,找不到函数的实现! 解决方法:在工程*.pro文 ...

  10. Linux下的ssh远程访问

    准备工作:首先需要在windows系统中安装虚拟机,并在虚拟机中安装好linux操作系统,这里安装的是vmware player虚拟机和ubuntu版本的操作系统.关于该部分的安装在作者的其他经验中有 ...