获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件
演示如何获取文件的缩略图
FileSystem/ThumbnailAccess.xaml
<Page x:Class="XamlDemo.FileSystem.ThumbnailAccess" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="using:XamlDemo.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"> <ScrollViewer Margin="120 0 0 0"> <StackPanel> <Button Name="btnGetThumbnail" Content="获取文件的缩略图" Click="btnGetThumbnail_Click_1" /> <Image Name="img" Stretch="None" HorizontalAlignment="Left" Margin="0 10 0 0" /> <TextBlock Name="lblMsg" FontSize="14.667" Margin="0 10 0 0" /> </StackPanel> </ScrollViewer> </Grid> </Page>
FileSystem/ThumbnailAccess.xaml.cs
/* * 演示如何获取文件的缩略图 * * 获取指定文件或文件夹的缩略图,返回 StorageItemThumbnail 类型的数据 * StorageFile.GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) * StorageFolder.GetThumbnailAsync(ThumbnailMode mode, uint requestedSize, ThumbnailOptions options) * ThumbnailMode mode - 用于描述缩略图的目的,以使系统确定缩略图图像的调整方式(PicturesView, VideosView, MusicView, DocumentsView, ListView, SingleItem) * 关于 ThumbnailMode 的详细介绍参见:http://msdn.microsoft.com/en-us/library/windows/apps/hh465350.aspx * uint requestedSize - 期望尺寸的最长边长的大小 * ThumbnailOptions options - 检索和调整缩略图的行为(None, ReturnOnlyIfCached, ResizeThumbnail, UseCurrentScale) */ using System; using Windows.Storage; using Windows.Storage.FileProperties; using Windows.Storage.Pickers; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Media.Imaging; namespace XamlDemo.FileSystem { public sealed partial class ThumbnailAccess : Page { public ThumbnailAccess() { this.InitializeComponent(); } private async void btnGetThumbnail_Click_1(object sender, RoutedEventArgs e) { if (XamlDemo.Common.Helper.EnsureUnsnapped()) { FileOpenPicker openPicker = new FileOpenPicker(); openPicker.FileTypeFilter.Add("*"); StorageFile file = await openPicker.PickSingleFileAsync(); if (file != null) { ThumbnailMode thumbnailMode = ThumbnailMode.PicturesView; ThumbnailOptions thumbnailOptions = ThumbnailOptions.UseCurrentScale; uint requestedSize = 200; using (StorageItemThumbnail thumbnail = await file.GetThumbnailAsync(thumbnailMode, requestedSize, thumbnailOptions)) { if (thumbnail != null) { BitmapImage bitmapImage = new BitmapImage(); bitmapImage.SetSource(thumbnail); img.Source = bitmapImage; lblMsg.Text = "file name: " + file.Name; lblMsg.Text += Environment.NewLine; lblMsg.Text += "requested size: " + requestedSize; lblMsg.Text += Environment.NewLine; lblMsg.Text += "returned size: " + thumbnail.OriginalWidth + "*" + thumbnail.OriginalHeight; } } } } } } }
4、演示如何通过 AQS - Advanced Query Syntax 搜索本地文件
FileSystem/AQS.xaml.cs
/* * 演示如何通过 AQS - Advanced Query Syntax 搜索本地文件 */ using System; using System.Collections.Generic; using Windows.Storage; using Windows.Storage.BulkAccess; using Windows.Storage.FileProperties; using Windows.Storage.Search; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace XamlDemo.FileSystem { public sealed partial class AQS : Page { public AQS() { this.InitializeComponent(); } protected async override void OnNavigatedTo(NavigationEventArgs e) { // 准备在“音乐库”中进行搜索(需要在 Package.appxmanifest 的“功能”中选中“音乐库”) StorageFolder musicFolder = KnownFolders.MusicLibrary; // 准备搜索所有类型的文件 List<string> fileTypeFilter = new List<string>(); fileTypeFilter.Add("*"); // 搜索的查询参数 QueryOptions queryOptions = new QueryOptions(CommonFileQuery.OrderByDate, fileTypeFilter); // 指定 AQS 字符串,参见 http://msdn.microsoft.com/zh-cn/library/windows/apps/aa965711.aspx queryOptions.UserSearchFilter = "五月天"; // 根据指定的参数创建一个查询 StorageFileQueryResult fileQuery = musicFolder.CreateFileQueryWithOptions(queryOptions); lblMsg.Text = "在音乐库中搜索“五月天”,结果如下:"; lblMsg.Text += Environment.NewLine; // 开始搜索,并返回检索到的文件列表 IReadOnlyList<StorageFile> files = await fileQuery.GetFilesAsync(); if (files.Count == 0) { lblMsg.Text += "什么都没搜到"; } else { foreach (StorageFile file in files) { lblMsg.Text += file.Name; lblMsg.Text += Environment.NewLine; } } // 关于 QueryOptions 的一些用法,更详细的 QueryOptions 的说明请参见 msdn queryOptions = new QueryOptions(); queryOptions.FolderDepth = FolderDepth.Deep; queryOptions.IndexerOption = IndexerOption.UseIndexerWhenAvailable; queryOptions.SortOrder.Clear(); var sortEntry = new SortEntry(); sortEntry.PropertyName = "System.FileName"; sortEntry.AscendingOrder = true; queryOptions.SortOrder.Add(sortEntry); fileQuery = KnownFolders.PicturesLibrary.CreateFileQueryWithOptions(queryOptions); } } }