[源码下载]

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

作者:webabcd

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

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

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

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

Demo.xaml.cs

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

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

<Page
x:Class="Windows81.FileSystem.StorageLibraryDemo"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.FileSystem"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <TextBlock Name="lblMsg" FontSize="14.667" /> <Button Name="btnAddFolder" Content="增加一个文件夹引用到图片库" Click="btnAddFolder_Click" Margin="0 10 0 0" /> <Button Name="btnRemoveFolder" Content="从图片库移除之前添加的全部文件夹引用" Click="btnRemoveFolder_Click" Margin="0 10 0 0" /> </StackPanel>
</Grid>
</Page>

StorageLibraryDemo.xaml.cs

/*
* 演示如何 添加/删除 “库”所包含的文件夹
*
* StorageLibrary - 用于“库”管理
* StorageLibrary.GetLibraryAsync(KnownLibraryId libraryId) - 静态方法,用于获取指定的“库”,返回 StorageLibrary 类型的对象
* Folders - 当前库所包含的文件夹们
* SaveFolder - 当前库的默认文件夹
* RequestAddFolderAsync() - 添加文件夹到当前库
* RequestRemoveFolderAsync() - 从当前库移除指定的文件夹
* DefinitionChanged - 当前库所包含的文件夹发生变化时触发的事件
*
*
* 关于文件系统和选择器的基础请见:
* http://www.cnblogs.com/webabcd/archive/2013/04/25/3041569.html
* http://www.cnblogs.com/webabcd/archive/2013/05/06/3062064.html
* http://www.cnblogs.com/webabcd/archive/2013/05/09/3068281.html
* http://www.cnblogs.com/webabcd/archive/2013/05/13/3075014.html
* http://www.cnblogs.com/webabcd/archive/2013/05/16/3081181.html
* http://www.cnblogs.com/webabcd/archive/2013/05/20/3087984.html
* http://www.cnblogs.com/webabcd/archive/2013/05/23/3094179.html
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows81.FileSystem
{
public sealed partial class StorageLibraryDemo : Page
{
// 临时保存添加进图片库的文件夹
private List<StorageFolder> _addedFloders = new List<StorageFolder>(); public StorageLibraryDemo()
{
this.InitializeComponent(); this.Loaded += StorageLibraryDemo_Loaded;
} async void StorageLibraryDemo_Loaded(object sender, RoutedEventArgs e)
{
// 注意:要想访问图片库,别忘了增加 <Capability Name="picturesLibrary" /> // 获取图片库的 StorageLibrary 对象
var picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); // 当前库所包含的文件夹增多或减少时
picturesLibrary.DefinitionChanged += async (StorageLibrary innerSender, object innerEvent) =>
{
await Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () =>
{
lblMsg.Text = "图片库所包含的文件夹如下:";
foreach (StorageFolder folder in picturesLibrary.Folders) // 当前库所包含的全部文件夹
{
lblMsg.Text += Environment.NewLine;
lblMsg.Text += folder.Path;
}
});
};
} // 增加一个文件夹引用到图片库
private async void btnAddFolder_Click(object sender, RoutedEventArgs e)
{
StorageLibrary picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); // 弹出文件夹选择器,以选择需要添加到图片库的文件夹
StorageFolder addedFolder = await picturesLibrary.RequestAddFolderAsync();
if (addedFolder != null)
{
// 添加成功
_addedFloders.Add(addedFolder);
}
else
{ }
} // 从图片库移除之前添加的全部文件夹引用
private async void btnRemoveFolder_Click(object sender, RoutedEventArgs e)
{
StorageLibrary picturesLibrary = await StorageLibrary.GetLibraryAsync(KnownLibraryId.Pictures); foreach (StorageFolder folder in _addedFloders)
{
// 从图片库移除指定的文件夹引用
if (await picturesLibrary.RequestRemoveFolderAsync(folder))
{
// 移除成功
}
else
{ }
}
}
}
}

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

<Page
x:Class="Windows81.FileSystem.Indexer"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="using:Windows81.FileSystem"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
mc:Ignorable="d"> <Grid Background="Transparent"> <StackPanel Margin="120 0 0 0"> <Button Name="btnAddToIndexer" Content="添加数据到索引器" Click="btnAddToIndexer_Click" /> <Button Name="btnRetrieveAllItems" Content="获取索引器中的全部数据" Click="btnRetrieveAllItems_Click" Margin="0 10 0 0" /> <Button Name="btnRetrieveMatchingItems" Content="按指定的查询条件获取索引器中的数据" Click="btnRetrieveMatchingItems_Click" Margin="0 10 0 0" /> <ScrollViewer Margin="0 10 0 0" Width="300" Height="400" HorizontalAlignment="Left">
<TextBlock Name="lblMsg" FontSize="14.667" />
</ScrollViewer> </StackPanel>
</Grid>
</Page>

Indexer.xaml.cs

/*
* 演示如何管理索引器,以及如何通过索引器获取数据
*
*
* 关于文件系统和选择器的基础请见:
* http://www.cnblogs.com/webabcd/archive/2013/04/25/3041569.html
* http://www.cnblogs.com/webabcd/archive/2013/05/06/3062064.html
* http://www.cnblogs.com/webabcd/archive/2013/05/09/3068281.html
* http://www.cnblogs.com/webabcd/archive/2013/05/13/3075014.html
* http://www.cnblogs.com/webabcd/archive/2013/05/16/3081181.html
* http://www.cnblogs.com/webabcd/archive/2013/05/20/3087984.html
* http://www.cnblogs.com/webabcd/archive/2013/05/23/3094179.html
*/ using System;
using System.Collections.Generic;
using Windows.Storage;
using Windows.Storage.Search;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls; namespace Windows81.FileSystem
{
public sealed partial class Indexer : Page
{
public Indexer()
{
this.InitializeComponent();
} // 添加数据到索引器
private async void btnAddToIndexer_Click(object sender, RoutedEventArgs e)
{
// 获取一个索引器(可以指定索引器的名字,从而达到对索引器分类的目的)
var indexer = ContentIndexer.GetIndexer();
var content = new IndexableContent();
for (int i = ; i < ; i++)
{
content.Properties[SystemProperties.Title] = "Title: " + i.ToString().PadLeft(, '');
content.Properties[SystemProperties.Keywords] = "Keywords: " + i.ToString().PadLeft(, ''); // 多个用“;”隔开
content.Properties[SystemProperties.Comment] = "Comment: " + i.ToString().PadLeft(, '');
content.Id = "key" + i; // 标识,增加同标识的索引就是更新 // 增加一个索引(另外还有 Update 和 Delete 操作)
await indexer.AddAsync(content);
}
} // 获取索引器中的全部数据
private void btnRetrieveAllItems_Click(object sender, RoutedEventArgs e)
{
ExecuteQueryHelper("*");
} // 按指定的查询条件获取索引器中的数据
private void btnRetrieveMatchingItems_Click(object sender, RoutedEventArgs e)
{
ExecuteQueryHelper("title:\"99\"");
} // 按指定的 AQS 语法从索引器中查询数据
private async void ExecuteQueryHelper(string queryString)
{
lblMsg.Text = "";
var indexer = ContentIndexer.GetIndexer(); string[] propertyKeys =
{
SystemProperties.Title,
SystemProperties.Keywords,
SystemProperties.Comment
};
// 通过 AQS 语法创建一个查询,关于 AQS 请参见:http://msdn.microsoft.com/zh-cn/library/windows/apps/aa965711.aspx
var query = indexer.CreateQuery(queryString, propertyKeys); // 执行查询,并获取结果
var documents = await query.GetAsync();
foreach (var document in documents)
{
string itemString = "Key: " + document.Id + "\n";
foreach (var propertyKey in propertyKeys)
{
itemString += propertyKey + ": " + StringifyProperty(document.Properties[propertyKey]) + "\n";
}
lblMsg.Text += itemString + "\n";
}
} // 如果对象是一个字符串集合则用“;”做分隔符,然后以字符串形式输出
public string StringifyProperty(object property)
{
string propertyString = "";
if (property != null)
{
var vectorProperty = property as IEnumerable<string>;
if (vectorProperty != null)
{
foreach (var prop in vectorProperty)
{
propertyString += prop + "; ";
}
}
else
{
propertyString = property.ToString();
}
}
return propertyString;
}
}
}

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. ubuntu-16.04+-xxx-i386.iso :安装 Oracle 11gR2 数据库

    前言:说实在的,ubuntu 16.04以上很难安装oracle!其间走过了艰难的一段路! 重要附件:ubuntu16.04+-xxx-i386.iso_安装oracle所需的软件包.zip 特点: ...

  2. SQLSERVER吞噬内存解决记录

    现在手上有一个不大不小的系统,运行了一段时间,因为是24*7不断运行,所以内存逐渐增高,慢慢的会飙到95%以上,然后不得不重启电脑,因为用的是云,怕虚拟机重启down掉起不来,重启操作还只能在凌晨4. ...

  3. friend class

    友元函数与友元类.   C++中以关键字friend声明友元关系.友元可以访问与其有friend关系的类中的私有成员.友元包括友元函数和友元类.   编辑本段1.友元函数 如果在本类以外的其它地方定义 ...

  4. 在多线程环境中使用CoreData

    在多线程环境中使用CoreData BY 子非鱼 · 2014 年 10 月 13 日   上回书说道,其实CoreData学起来也没有很复杂,我们其实增删改查都和别的ORM大同小异.但是世界总是很复 ...

  5. Unity Remote 4安卓机使用指南

    必须U3D版本为4.5以上,可以在Public目录下载.想实时调试IOS版本必须是MAC系统! 优点:可以在不编译的情况下实时的去调试真实Android设备的各种情况,包括使用触摸功能(Remote接 ...

  6. 电商大促准备流程v2

    1 概述 对于电商企业而言,每年都会有几次大的促销活动,像双十一.店庆等,对于第一次参加这个活动的新手,难免会有些没有头绪,因而将自己参加双十一.双十二活动中的过程心得进行下总结,一方面供以后工作中继 ...

  7. 十一、EnterpriseFrameWork框架的分层与系统业务的结合

    上章详细讲了EnterpriseFrameWork框架中的每个分层,这都是从技术层面来说明,也就是我们知道怎么来建一个控制器或一个业务对象,但开发过程中应该建一个什么样的控制器或业务对象了?本章的主要 ...

  8. Android中使用自身携带的Junit新建一个测试工程

    1.新建立一个Android工程 package com.shellway.junit; public class Service { public int divide(int a,int b){ ...

  9. [转]不定义JQuery插件,不要说会JQuery

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人,直 ...

  10. Lua基础

    局部定义与代码块: 使用local声明一个局部变量或局部函数,局部对象只在被声明的那个代码块中有效. 代码块:一个控制结构.一个函数体.一个chunk(一个文件或文本串)(Lua把chunk当做函数处 ...