zoukankan      html  css  js  c++  java
  • wp8.1 Study10:APP数据存储

    一、理论

    1、App的各种数据在WP哪里的?

    下图很好介绍了这个问题。有InstalltionFolder, knownFolder, SD Card...

    2、一个App数据存储概览

    主要分两大部分,InstallationFolder和App Data Folder

    3、Windows.Storage.ApplicationData  和  Windows.Security.Credentials简述

    其中利用Windows.Storage.ApplicationData,我们可以获得3种不同的wp中文件夹和2种设置保存字典,C#操作如下

     Windows.Storage.StorageFolder roam = Windows.Storage.ApplicationData.Current.RoamingFolder;
     Windows.Storage.StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;
     Windows.Storage.StorageFolder temp = Windows.Storage.ApplicationData.Current.TemporaryFolder;
    
     Windows.Storage.ApplicationDataContainer localSettings = Windows.Storage.ApplicationData.Current.LocalSettings;
     Windows.Storage.ApplicationDataContainer roamingSettings = Windows.Storage.ApplicationData.Current.RoamingSettings;

    二、Local Folder 和 Local Setting

    1、LocalSetting使用,C#代码如下:

    // Create a simple setting
    localSettings.Values["exampleSetting"] = "Hello Windows";
    
    // Read data from a simple setting
    Object value = localSettings.Values["exampleSetting"];
    
    if (value == null)
    {
        // No data
    }
    else
    {
        // Access data in value
    }
    
    // Delete a simple setting
    localSettings.Values.Remove("exampleSetting");

    2、LocalFolder使用,C#代码如下:

      private async void writeTextToLocalStorageFile(string filename, string text)
            {
                var fold = Windows.Storage.ApplicationData.Current.LocalFolder;//打开文件夹
                StorageFile file = await fold.CreateFileAsync(filename, CreationCollisionOption.ReplaceExisting);//创建个文件
                await FileIO.WriteTextAsync(file, text);//在文件里面写内容
            }
    
            private async Task<string> readTextFromLocalStorage(string filename)
            {
                var fold = Windows.Storage.ApplicationData.Current.LocalFolder;//打开文件夹
                StorageFile file = await fold.GetFileAsync(filename);//打开对应的文件
                string result = await FileIO.ReadTextAsync(file);//读取文件里面的内容
                return result;
            }

    Note:这里对文件的操作是十分简单的,步骤是在电脑管理文件的步骤是类似的。

    三、Roaming Setting 和Roaming Folder

    如果用户在多个设备上安装了你的程序,如果所有的设备可以共享相同的设置信息是很好的体验。而Roaming data提供了一个应用程序可以在多个不同的物理设备同步数据和设置方法。它以一个文件夹和设置字典的形式自动存储在用户的OneDrive。Roaming data的大小限制于ApplicationData.RoamingStorageQuota(一般是100k左右,但它不会占用你Onedrive的空间大小)。其同步过程图如下:

      Roaming Setting 和Roaming Folder的操作方式与上面的Local Folder 和 Local Setting操作方式一样,在这不一一介绍。但是需要在不同设备中监视Roaming data的改变事件。C#代码如下:

    Windows.Storage.ApplicationData.Current.DataChanged += Current_DataChanged;
    ...
    
    void Current_DataChanged(ApplicationData sender, object args)
    {
        // Refresh your settings...
    }

      数据的同步是发生在后台的。

    四、几种不同方式获取文件的方式

  • 相关阅读:
    Java阶段测试题一
    HttpClient配置及运用(一)
    字符串数组及链表的应用:例题
    Java多线程
    String普通方法测试;可变参数
    Java连接mysql数据库
    JS练习
    foreach遍历、包装类、Object类
    Java多态总结
    类的关联,不同类属性的调用
  • 原文地址:https://www.cnblogs.com/NEIL-X/p/4164048.html
Copyright © 2011-2022 走看看