private async void UpdatePianoSheetsFromFolder(StorageFolder searchFolder) { lock (_syncIsBusy) { if (IsBusy) { return; } else { IsBusy = true; } } LoadingMsg = "Loading..."; PianoSheets.Clear(); SearchFolderPath = searchFolder.Path.Length > 0 ? searchFolder.Path : searchFolder.Name; StorageFolder temporaryFolder = await KnownFolders.PicturesLibrary.CreateFolderAsync("PianoSheetViewer", CreationCollisionOption.ReplaceExisting); await ConvertPortableDocumentFormatFilesAsync(searchFolder, temporaryFolder); await GetPianoSheetsAsync(true, temporaryFolder); await GetPianoSheetsAsync(false, searchFolder); await Task.Delay(1000); lock (_syncIsBusy) { IsBusy = false; } }
private async Task GetPianoSheetsAsync(bool isPortableDocumentFormatFile, StorageFolder storageFolder) { QueryOptions options = new QueryOptions(); options.FolderDepth = FolderDepth.Deep; options.FileTypeFilter.Add(".jpg"); options.FileTypeFilter.Add(".png"); options.FileTypeFilter.Add(".gif"); StorageFileQueryResult query = storageFolder.CreateFileQueryWithOptions(options); IReadOnlyList <StorageFile> imageFiles = await query.GetFilesAsync(); Dictionary <string, List <StorageFile> > parentFolderPathToImages = new Dictionary <string, List <StorageFile> >(); foreach (StorageFile file in imageFiles) { string parentFolderPath = file.Path.Replace(file.Name, ""); if (parentFolderPathToImages.ContainsKey(parentFolderPath)) { parentFolderPathToImages[parentFolderPath].Add(file); } else { parentFolderPathToImages.Add(parentFolderPath, new List <StorageFile> { file }); } } foreach (KeyValuePair <string, List <StorageFile> > kvp in parentFolderPathToImages) { kvp.Value.Sort((file1, file2) => file1.Name.CompareTo(file2.Name)); string name = kvp.Key; string[] parentFolderPathSplited = kvp.Key.Split(new[] { Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar }); if (parentFolderPathSplited.Length > 1) { name = parentFolderPathSplited[parentFolderPathSplited.Length - 2]; } string fileType = String.Format("Images 📄×{0}", kvp.Value.Count); if (isPortableDocumentFormatFile) { fileType = String.Format("PDF Document 📄×{0}", kvp.Value.Count); } ImageProperties properties = await kvp.Value.First().Properties.GetImagePropertiesAsync(); PianoSheetInfo pianoSheetInfo = new PianoSheetInfo(kvp.Value.First(), kvp.Value, name, fileType, properties); PianoSheets.Add(pianoSheetInfo); } }