private async Task ReloadFolderAsync(string path, CancellationToken cancellationToken)
        {
            //currentPath = path;

            folderQuery.ApplyNewQueryOptions(folderQueryOptions);
            fileQuery.ApplyNewQueryOptions(fileQueryOptions);

            Clear();
            await LoadFoldersAsync(cancellationToken);
            await LoadFilesAsync(cancellationToken);

            s.Stop();
            Debug.WriteLine("Load took: " + s.ElapsedMilliseconds + "ms");
        }
示例#2
0
        public async Task <IEnumerable <MenuCard> > ReadMenuCardsAsync()
        {
            List <MenuCard> menuCards = new List <MenuCard>();
            StorageFolder   folder    = ApplicationData.Current.RoamingFolder;

            StorageFileQueryResult result = folder.CreateFileQuery();
            var queryOptions = new QueryOptions();

            queryOptions.IndexerOption = IndexerOption.DoNotUseIndexer;
            queryOptions.FolderDepth   = FolderDepth.Shallow;
            queryOptions.FileTypeFilter.Add(".xml");
            result.ApplyNewQueryOptions(queryOptions);
            IReadOnlyList <StorageFile> files = await result.GetFilesAsync();

            foreach (var file in files)
            {
                using (Stream stream = await file.OpenStreamForReadAsync())
                {
                    try
                    {
                        var    serializer = new DataContractSerializer(typeof(MenuCardData));
                        object data       = await Task <object> .Run(() => serializer.ReadObject(stream));

                        //var serializer = new XmlSerializer(typeof(MenuCardData));
                        //object data = await Task<object>.Run(() => serializer.Deserialize(stream));
                        MenuCard menuCard = (data as MenuCardData).ToMenuCard();
                        menuCard.RestoreReferences();
                        menuCards.Add(menuCard);
                    }
                    catch (Exception)
                    {
                        // TODO: log exception
                    }
                }
            }

            // read images
            MenuCardImageStorage imageStorage = new MenuCardImageStorage();

            foreach (var menuCard in menuCards)
            {
                if (menuCard.ImagePath != null)
                {
                    menuCard.Image = await imageStorage.ReadImageAsync(menuCard.ImagePath);
                }
            }
            return(menuCards);
        }
示例#3
0
        private async void FileContentsChanged(IStorageQueryResultBase sender, object args)
        {
            if (_filesRefreshing)
            {
                Debug.WriteLine("Filesystem change event fired but refresh is already running");
                return;
            }
            else
            {
                Debug.WriteLine("Filesystem change event fired. Refreshing...");
            }

            _filesRefreshing = true;

            //query options have to be reapplied otherwise old results are returned
            _fileQueryResult.ApplyNewQueryOptions(_options);
            _folderQueryResult.ApplyNewQueryOptions(_options);

            var fileCount = await _fileQueryResult.GetItemCountAsync();

            var folderCount = await _folderQueryResult.GetItemCountAsync();

            var files = await _fileQueryResult.GetFilesAsync();

            var folders = await _folderQueryResult.GetFoldersAsync();

            var cancellationTokenSourceCopy = _cancellationTokenSource;

            // modifying a file also results in a new unique FolderRelativeId so no need to check for DateModified explicitly

            var addedFiles             = files.Select(f => f.FolderRelativeId).Except(_filesAndFolders.Select(f => f.FolderRelativeId));
            var addedFolders           = folders.Select(f => f.FolderRelativeId).Except(_filesAndFolders.Select(f => f.FolderRelativeId));
            var removedFilesAndFolders = _filesAndFolders
                                         .Select(f => f.FolderRelativeId)
                                         .Except(files.Select(f => f.FolderRelativeId))
                                         .Except(folders.Select(f => f.FolderRelativeId))
                                         .ToArray();

            foreach (var file in addedFiles)
            {
                var toAdd = files.First(f => f.FolderRelativeId == file);
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              async() =>
                {
                    await AddFile(toAdd, _pageName, cancellationTokenSourceCopy.Token);
                });
            }
            foreach (var folder in addedFolders)
            {
                var toAdd = folders.First(f => f.FolderRelativeId == folder);
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              async() =>
                {
                    await AddFolder(toAdd, _pageName, cancellationTokenSourceCopy.Token);
                });
            }
            foreach (var item in removedFilesAndFolders)
            {
                var toRemove = _filesAndFolders.First(f => f.FolderRelativeId == item);
                await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
                                                                              () =>
                {
                    RemoveFileOrFolder(toRemove);
                });
            }

            _filesRefreshing = false;
            Debug.WriteLine("Filesystem refresh complete");
        }