重新想象 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 > 0) { 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 = 0; i < 100; i++) { content.Properties[SystemProperties.Title] = "Title: " + i.ToString().PadLeft(2, '0'); content.Properties[SystemProperties.Keywords] = "Keywords: " + i.ToString().PadLeft(2, '0'); // 多個用“;”隔開 content.Properties[SystemProperties.Comment] = "Comment: " + i.ToString().PadLeft(2, '0'); 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
[源碼下載]
