[源码下载]

重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性

作者:webabcd

介绍
重新想象 Windows 8.1 Store Apps 之文件系统的新特性

  • 简要说明 win8.1 中关于文件系统的增强
  • “库”管理
  • 管理以及使用索引

示例
1、简要说明 win8.1 中关于文件系统的增强
Demo.xaml

  1. <Page
  2. x:Class="Windows81.FileSystem.Demo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows81.FileSystem"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <StackPanel Margin="120 0 0 0">
  13.  
  14. <TextBlock Name="lblMsg" FontSize="14.667" />
  15.  
  16. <TextBlock FontSize="14.667" Text="本例简要说明了 win8.1 中关于文件系统的增强,详见后台代码中的说明" Margin="0 10 0 0" />
  17.  
  18. </StackPanel>
  19. </Grid>
  20. </Page>

Demo.xaml.cs

  1. /*
  2. * 简要说明 win8.1 中关于文件系统的增强
  3. *
  4. *
  5. * 关于文件系统和选择器的基础请见:
  6. * http://www.cnblogs.com/webabcd/archive/2013/04/25/3041569.html
  7. * http://www.cnblogs.com/webabcd/archive/2013/05/06/3062064.html
  8. * http://www.cnblogs.com/webabcd/archive/2013/05/09/3068281.html
  9. * http://www.cnblogs.com/webabcd/archive/2013/05/13/3075014.html
  10. * http://www.cnblogs.com/webabcd/archive/2013/05/16/3081181.html
  11. * http://www.cnblogs.com/webabcd/archive/2013/05/20/3087984.html
  12. * http://www.cnblogs.com/webabcd/archive/2013/05/23/3094179.html
  13. */
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using System.Linq;
  18. using Windows.Storage;
  19. using Windows.UI.Xaml.Controls;
  20.  
  21. namespace Windows81.FileSystem
  22. {
  23. public sealed partial class Demo : Page
  24. {
  25. public Demo()
  26. {
  27. this.InitializeComponent();
  28.  
  29. Comment();
  30. }
  31.  
  32. private async void Comment()
  33. {
  34. // 1、在拆分屏幕状态下,打开文件选取器时,如果当前拆分屏有一定的宽度,则文件选取器会在当前拆分屏显示,而无需全屏显示
  35.  
  36. // 2、StorageFolder 和 StorageFile 都实现了 IStorageItem2 接口,其有一个 GetParentAsync() 方法用于获取当前 StorageFolder 或 StorageFile 的父文件夹
  37. StorageFolder storageFolder = KnownFolders.DocumentsLibrary; // 在 win8.1 中访问 DocumentsLibrary 除了要添加 <Capability Name="documentsLibrary" /> 外,还要有相应的文件关联才行
  38. IReadOnlyList<StorageFolder> folders = await storageFolder.GetFoldersAsync();
  39. if (folders.Count > )
  40. {
  41. StorageFolder folder = folders.First();
  42. StorageFolder parentFolder = await folder.GetParentAsync(); // 获取父亲文件夹(如果没有权限的话会返回 null)
  43. lblMsg.Text = parentFolder.Name;
  44. }
  45.  
  46. // 3、StorageFolder 和 StorageFile 都实现了 IStorageItem2 接口,其有一个 IsEqual() 方法用于判断两个 IStorageItem2 是否相等
  47. // 另外补充一个在 win8 中忘了写的一个知识点,判断一个 IStorageItem 是 StorageFolder 还是 StorageFile 可以通过 IsOfType(StorageItemTypes type) 方法来判断
  48.  
  49. // 4、KnownFolders 新增了两个属性,如下:
  50. // KnownFolders.CameraRoll
  51. // KnownFolders.Playlists
  52.  
  53. // 5、新增了 StorageFolder.TryGetItemAsync(string name) 方法,不用再自己写 try catch 了(但是个别异常还是会抛出的,建议还是自己写帮助类吧)
  54. // StorageFolder.TryGetItemAsync(string name)
  55.  
  56. // 6、文件激活应用程序时,其事件参数 FileActivatedEventArgs 新增了 NeighboringFilesQuery 属性,用于获取激活文件附近的文件们
  57.  
  58. // 7、文件选择器中集成了 OneDrive
  59. }
  60. }
  61. }

2、演示如何 添加/删除 “库”所包含的文件夹
StorageLibraryDemo.xaml

  1. <Page
  2. x:Class="Windows81.FileSystem.StorageLibraryDemo"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows81.FileSystem"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <StackPanel Margin="120 0 0 0">
  13.  
  14. <TextBlock Name="lblMsg" FontSize="14.667" />
  15.  
  16. <Button Name="btnAddFolder" Content="增加一个文件夹引用到图片库" Click="btnAddFolder_Click" Margin="0 10 0 0" />
  17.  
  18. <Button Name="btnRemoveFolder" Content="从图片库移除之前添加的全部文件夹引用" Click="btnRemoveFolder_Click" Margin="0 10 0 0" />
  19.  
  20. </StackPanel>
  21. </Grid>
  22. </Page>

StorageLibraryDemo.xaml.cs

  1. /*
  2. * 演示如何 添加/删除 “库”所包含的文件夹
  3. *
  4. * StorageLibrary - 用于“库”管理
  5. * StorageLibrary.GetLibraryAsync(KnownLibraryId libraryId) - 静态方法,用于获取指定的“库”,返回 StorageLibrary 类型的对象
  6. * Folders - 当前库所包含的文件夹们
  7. * SaveFolder - 当前库的默认文件夹
  8. * RequestAddFolderAsync() - 添加文件夹到当前库
  9. * RequestRemoveFolderAsync() - 从当前库移除指定的文件夹
  10. * DefinitionChanged - 当前库所包含的文件夹发生变化时触发的事件
  11. *
  12. *
  13. * 关于文件系统和选择器的基础请见:
  14. * http://www.cnblogs.com/webabcd/archive/2013/04/25/3041569.html
  15. * http://www.cnblogs.com/webabcd/archive/2013/05/06/3062064.html
  16. * http://www.cnblogs.com/webabcd/archive/2013/05/09/3068281.html
  17. * http://www.cnblogs.com/webabcd/archive/2013/05/13/3075014.html
  18. * http://www.cnblogs.com/webabcd/archive/2013/05/16/3081181.html
  19. * http://www.cnblogs.com/webabcd/archive/2013/05/20/3087984.html
  20. * http://www.cnblogs.com/webabcd/archive/2013/05/23/3094179.html
  21. */
  22.  
  23. using System;
  24. using System.Collections.Generic;
  25. using Windows.Storage;
  26. using Windows.UI.Xaml;
  27. using Windows.UI.Xaml.Controls;
  28.  
  29. namespace Windows81.FileSystem
  30. {
  31. public sealed partial class StorageLibraryDemo : Page
  32. {
  33. // 临时保存添加进图片库的文件夹
  34. private List<StorageFolder> _addedFloders = new List<StorageFolder>();
  35.  
  36. public StorageLibraryDemo()
  37. {
  38. this.InitializeComponent();
  39.  
  40. this.Loaded += StorageLibraryDemo_Loaded;
  41. }
  42.  
  43. async void StorageLibraryDemo_Loaded(object sender, RoutedEventArgs e)
  44. {
  45. // 注意:要想访问图片库,别忘了增加 <Capability Name="picturesLibrary" />
  46.  
  47. // 获取图片库的 StorageLibrary 对象
  48. var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
  49.  
  50. // 当前库所包含的文件夹增多或减少时
  51. picturesLibrary.DefinitionChanged += async (StorageLibrary innerSender, object innerEvent) =>
  52. {
  53. await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
  54. {
  55. lblMsg.Text = "图片库所包含的文件夹如下:";
  56. foreach (StorageFolder folder in picturesLibrary.Folders) // 当前库所包含的全部文件夹
  57. {
  58. lblMsg.Text += Environment.NewLine;
  59. lblMsg.Text += folder.Path;
  60. }
  61. });
  62. };
  63. }
  64.  
  65. // 增加一个文件夹引用到图片库
  66. private async void btnAddFolder_Click(object sender, RoutedEventArgs e)
  67. {
  68. StorageLibrary picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
  69.  
  70. // 弹出文件夹选择器,以选择需要添加到图片库的文件夹
  71. StorageFolder addedFolder = await picturesLibrary.RequestAddFolderAsync();
  72. if (addedFolder != null)
  73. {
  74. // 添加成功
  75. _addedFloders.Add(addedFolder);
  76. }
  77. else
  78. {
  79.  
  80. }
  81. }
  82.  
  83. // 从图片库移除之前添加的全部文件夹引用
  84. private async void btnRemoveFolder_Click(object sender, RoutedEventArgs e)
  85. {
  86. StorageLibrary picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures);
  87.  
  88. foreach (StorageFolder folder in _addedFloders)
  89. {
  90. // 从图片库移除指定的文件夹引用
  91. if (await picturesLibrary.RequestRemoveFolderAsync(folder))
  92. {
  93. // 移除成功
  94. }
  95. else
  96. {
  97.  
  98. }
  99. }
  100. }
  101. }
  102. }

3、演示如何管理索引器,以及如何通过索引器获取数据
Indexer.xaml

  1. <Page
  2. x:Class="Windows81.FileSystem.Indexer"
  3. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  4. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  5. xmlns:local="using:Windows81.FileSystem"
  6. xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  7. xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  8. mc:Ignorable="d">
  9.  
  10. <Grid Background="Transparent">
  11.  
  12. <StackPanel Margin="120 0 0 0">
  13.  
  14. <Button Name="btnAddToIndexer" Content="添加数据到索引器" Click="btnAddToIndexer_Click" />
  15.  
  16. <Button Name="btnRetrieveAllItems" Content="获取索引器中的全部数据" Click="btnRetrieveAllItems_Click" Margin="0 10 0 0" />
  17.  
  18. <Button Name="btnRetrieveMatchingItems" Content="按指定的查询条件获取索引器中的数据" Click="btnRetrieveMatchingItems_Click" Margin="0 10 0 0" />
  19.  
  20. <ScrollViewer Margin="0 10 0 0" Width="300" Height="400" HorizontalAlignment="Left">
  21. <TextBlock Name="lblMsg" FontSize="14.667" />
  22. </ScrollViewer>
  23.  
  24. </StackPanel>
  25. </Grid>
  26. </Page>

Indexer.xaml.cs

  1. /*
  2. * 演示如何管理索引器,以及如何通过索引器获取数据
  3. *
  4. *
  5. * 关于文件系统和选择器的基础请见:
  6. * http://www.cnblogs.com/webabcd/archive/2013/04/25/3041569.html
  7. * http://www.cnblogs.com/webabcd/archive/2013/05/06/3062064.html
  8. * http://www.cnblogs.com/webabcd/archive/2013/05/09/3068281.html
  9. * http://www.cnblogs.com/webabcd/archive/2013/05/13/3075014.html
  10. * http://www.cnblogs.com/webabcd/archive/2013/05/16/3081181.html
  11. * http://www.cnblogs.com/webabcd/archive/2013/05/20/3087984.html
  12. * http://www.cnblogs.com/webabcd/archive/2013/05/23/3094179.html
  13. */
  14.  
  15. using System;
  16. using System.Collections.Generic;
  17. using Windows.Storage;
  18. using Windows.Storage.Search;
  19. using Windows.UI.Xaml;
  20. using Windows.UI.Xaml.Controls;
  21.  
  22. namespace Windows81.FileSystem
  23. {
  24. public sealed partial class Indexer : Page
  25. {
  26. public Indexer()
  27. {
  28. this.InitializeComponent();
  29. }
  30.  
  31. // 添加数据到索引器
  32. private async void btnAddToIndexer_Click(object sender, RoutedEventArgs e)
  33. {
  34. // 获取一个索引器(可以指定索引器的名字,从而达到对索引器分类的目的)
  35. var indexer = ContentIndexer.GetIndexer();
  36. var content = new IndexableContent();
  37. for (int i = ; i < ; i++)
  38. {
  39. content.Properties[SystemProperties.Title] = "Title: " + i.ToString().PadLeft(, '');
  40. content.Properties[SystemProperties.Keywords] = "Keywords: " + i.ToString().PadLeft(, ''); // 多个用“;”隔开
  41. content.Properties[SystemProperties.Comment] = "Comment: " + i.ToString().PadLeft(, '');
  42. content.Id = "key" + i; // 标识,增加同标识的索引就是更新
  43.  
  44. // 增加一个索引(另外还有 Update 和 Delete 操作)
  45. await indexer.AddAsync(content);
  46. }
  47. }
  48.  
  49. // 获取索引器中的全部数据
  50. private void btnRetrieveAllItems_Click(object sender, RoutedEventArgs e)
  51. {
  52. ExecuteQueryHelper("*");
  53. }
  54.  
  55. // 按指定的查询条件获取索引器中的数据
  56. private void btnRetrieveMatchingItems_Click(object sender, RoutedEventArgs e)
  57. {
  58. ExecuteQueryHelper("title:\"99\"");
  59. }
  60.  
  61. // 按指定的 AQS 语法从索引器中查询数据
  62. private async void ExecuteQueryHelper(string queryString)
  63. {
  64. lblMsg.Text = "";
  65. var indexer = ContentIndexer.GetIndexer();
  66.  
  67. string[] propertyKeys =
  68. {
  69. SystemProperties.Title,
  70. SystemProperties.Keywords,
  71. SystemProperties.Comment
  72. };
  73. // 通过 AQS 语法创建一个查询,关于 AQS 请参见:http://msdn.microsoft.com/zh-cn/library/windows/apps/aa965711.aspx
  74. var query = indexer.CreateQuery(queryString, propertyKeys);
  75.  
  76. // 执行查询,并获取结果
  77. var documents = await query.GetAsync();
  78. foreach (var document in documents)
  79. {
  80. string itemString = "Key: " + document.Id + "\n";
  81. foreach (var propertyKey in propertyKeys)
  82. {
  83. itemString += propertyKey + ": " + StringifyProperty(document.Properties[propertyKey]) + "\n";
  84. }
  85. lblMsg.Text += itemString + "\n";
  86. }
  87. }
  88.  
  89. // 如果对象是一个字符串集合则用“;”做分隔符,然后以字符串形式输出
  90. public string StringifyProperty(object property)
  91. {
  92. string propertyString = "";
  93. if (property != null)
  94. {
  95. var vectorProperty = property as IEnumerable<string>;
  96. if (vectorProperty != null)
  97. {
  98. foreach (var prop in vectorProperty)
  99. {
  100. propertyString += prop + "; ";
  101. }
  102. }
  103. else
  104. {
  105. propertyString = property.ToString();
  106. }
  107. }
  108. return propertyString;
  109. }
  110. }
  111. }

OK
[源码下载]

重新想象 Windows 8.1 Store Apps (83) - 文件系统的新特性的更多相关文章

  1. 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (84) - 图像处理的新特性, Share Contract 的新特性 作者:webabcd 介绍重新想象 Windows 8. ...

  2. 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient

    [源码下载] 重新想象 Windows 8.1 Store Apps (88) - 通信的新特性: 新的 HttpClient 作者:webabcd 介绍重新想象 Windows 8.1 Store ...

  3. 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件

    [源码下载] 重新想象 Windows 8.1 Store Apps (89) - 通信的新特性: 下载数据, 上传数据, 上传文件 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

  4. 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cookie 读写; 自定义 HttpFilter; 其他

    [源码下载] 重新想象 Windows 8.1 Store Apps (90) - 通信的新特性: 通过 HttpBaseProtocolFilter 实现 http 请求的缓存控制,以及 cooki ...

  5. 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (91) - 后台任务的新特性: 下载和上传的新特性, 程序启动前预下载网络资源, 后台任务的其它新特性 作者:webabcd 介 ...

  6. 重新想象 Windows 8.1 Store Apps 系列文章索引

    [源码下载] [重新想象 Windows 8 Store Apps 系列文章] 重新想象 Windows 8.1 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  7. 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性

    [源码下载] 重新想象 Windows 8.1 Store Apps (85) - 警报通知(闹钟), Tile 的新特性 作者:webabcd 介绍重新想象 Windows 8.1 Store Ap ...

  8. 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Contract 分享 WebView 中的内容, 为 WebView 截图

    [源码下载] 重新想象 Windows 8.1 Store Apps (81) - 控件增强: WebView 之加载本地 html, 智能替换 html 中的 url 引用, 通过 Share Co ...

  9. 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar

    [源码下载] 重新想象 Windows 8.1 Store Apps (72) - 新增控件: AppBar, CommandBar 作者:webabcd 介绍重新想象 Windows 8.1 Sto ...

随机推荐

  1. 关于把本地应用封装成windows app发布审核通不过的问题

    把传统的b/s系统,简单改版,做成了一个比较适合于领导查询的系统,并开发了一个app程序封装了webview直接导向该程序,无需登陆直接访问:结果在提交app的时候审核通不过,问题是安全审核失败: 大 ...

  2. Windows Live Writer配置步骤

    推荐文档: [超详细教程]使用Windows Live Writer 2012和Office Word 2013 发布文章到博客园全面总结 Live Writer 使用小贴示:发博客时始终使用图片原始 ...

  3. 个人软件过程5 git命令行方式超简洁教程

    虽然许多IDE对git的支持不错,但用命令行方式,有助于对git本身的理解.这里对实际工作中,使用git的流程,以及与其相关的命令 小结一下,基本上,掌握这些命令,就能自如的在工作中使用. 1.git ...

  4. iOS工程集成支付宝错误Undefined symbols for architecture armv7

    问题描述: 新工程中需要集成支付宝功能,于是咱就把支付宝的库给集成了进入然后就出现了下面这种错误了说,错误信息如下: Undefined symbols for architecture armv7: ...

  5. LINUNX下PHP下载中文文件名代码

            function get_basename($filename){                 return preg_replace('/^.+[\\\\\\/]/', '',  ...

  6. PLSQL登录弹出空白框如何解决

     转自:http://jingyan.baidu.com/article/066074d6760959c3c21cb0d6.html   出现登录弹出空白框这是由于win7的安全性提高了,在PLSQL ...

  7. 深入解析Oracle 10g中SGA_MAX_SIZE和SGA_TARGET参数的区别和作用

    原文链接:http://m.blog.csdn.net/blog/aaron8219/40037005 SGA_MAX_SIZE是从9i以来就有的作为设置SGA大小的一个参数,而SGA_TARGET则 ...

  8. Red Hat Enterprise Linux 各版本详细说明

    https://access.redhat.com/articles/3078#RHEL7 Red Hat Enterprise Linux Release Dates Updated Novembe ...

  9. MVC + EF + Bootstrap 2 权限管理系统入门级(附源码)

    MVC .EF 学习有大半年了,用的还不是很熟练,正好以做这样一个简单的权限管理系统作为学习的切入点,还是非常合适的. 开发环境: VS 2013 + Git + MVC 5 + EF 6 Code ...

  10. QTP对象管理

    QTP对象库管理 - 动态绑定对象库文件:http://blog.csdn.net/testing_is_ ... le/details/20569843 用ObjectRepositoryUtil动 ...