示例#1
0
        private async Task AddItemToRecentListAsync(IStorageItem item, Windows.Storage.AccessCache.AccessListEntry entry)
        {
            BitmapImage      ItemImage;
            string           ItemPath;
            string           ItemName;
            StorageItemTypes ItemType;
            Visibility       ItemFolderImgVis;
            Visibility       ItemEmptyImgVis;
            Visibility       ItemFileIconVis;

            if (item.IsOfType(StorageItemTypes.File))
            {
                // Try to read the file to check if still exists
                // This is only needed to remove files opened from a disconnected android/MTP phone
                if (string.IsNullOrEmpty(item.Path)) // This indicates that the file was open from an MTP device
                {
                    using (var inputStream = await((StorageFile)item).OpenReadAsync())
                        using (var classicStream = inputStream.AsStreamForRead())
                            using (var streamReader = new StreamReader(classicStream))
                            {
                                // NB: this might trigger the download of the file from OneDrive
                                streamReader.Peek();
                            }
                }

                ItemName  = item.Name;
                ItemPath  = string.IsNullOrEmpty(item.Path) ? entry.Metadata : item.Path;
                ItemType  = StorageItemTypes.File;
                ItemImage = new BitmapImage();
                StorageFile file      = (StorageFile)item;
                var         thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 30, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);

                if (thumbnail == null)
                {
                    ItemEmptyImgVis = Visibility.Visible;
                }
                else
                {
                    await ItemImage.SetSourceAsync(thumbnail.CloneStream());

                    ItemEmptyImgVis = Visibility.Collapsed;
                }
                ItemFolderImgVis = Visibility.Collapsed;
                ItemFileIconVis  = Visibility.Visible;
                recentItemsCollection.Add(new RecentItem()
                {
                    RecentPath  = ItemPath,
                    Name        = ItemName,
                    Type        = ItemType,
                    FolderImg   = ItemFolderImgVis,
                    EmptyImgVis = ItemEmptyImgVis,
                    FileImg     = ItemImage,
                    FileIconVis = ItemFileIconVis
                });
            }
        }
示例#2
0
        public async void PopulateRecentsList()
        {
            var              mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
            BitmapImage      ItemImage        = new BitmapImage();
            string           ItemPath         = null;
            string           ItemName;
            StorageItemTypes ItemType;
            Visibility       ItemFolderImgVis;
            Visibility       ItemEmptyImgVis;
            Visibility       ItemFileIconVis;

            if (mostRecentlyUsed.Entries.Count == 0)
            {
                Empty.Visibility = Visibility.Visible;
            }
            else
            {
                Empty.Visibility = Visibility.Collapsed;
            }
            foreach (Windows.Storage.AccessCache.AccessListEntry entry in mostRecentlyUsed.Entries)
            {
                string mruToken = entry.Token;
                try
                {
                    Windows.Storage.IStorageItem item = await mostRecentlyUsed.GetItemAsync(mruToken);

                    if (item.IsOfType(StorageItemTypes.File))
                    {
                        ItemName  = item.Name;
                        ItemPath  = item.Path;
                        ItemType  = StorageItemTypes.File;
                        ItemImage = new BitmapImage();
                        StorageFile file = await StorageFile.GetFileFromPathAsync(ItemPath);

                        var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.ListView, 30, Windows.Storage.FileProperties.ThumbnailOptions.ResizeThumbnail);

                        if (thumbnail == null)
                        {
                            ItemEmptyImgVis = Visibility.Visible;
                        }
                        else
                        {
                            await ItemImage.SetSourceAsync(thumbnail.CloneStream());

                            ItemEmptyImgVis = Visibility.Collapsed;
                        }
                        ItemFolderImgVis = Visibility.Collapsed;
                        ItemFileIconVis  = Visibility.Visible;
                        recentItemsCollection.Add(new RecentItem()
                        {
                            path = ItemPath, name = ItemName, type = ItemType, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis
                        });
                    }
                }
                catch (System.IO.FileNotFoundException)
                {
                    mostRecentlyUsed.Remove(mruToken);
                }
                catch (UnauthorizedAccessException)
                {
                    // Skip item until consent is provided
                }
            }
        }
示例#3
0
        public async void PopulateRecentsList()
        {
            var              mostRecentlyUsed = Windows.Storage.AccessCache.StorageApplicationPermissions.MostRecentlyUsedList;
            BitmapImage      ItemImage;
            string           ItemPath;
            string           ItemName;
            StorageItemTypes ItemType;
            Visibility       ItemFolderImgVis;
            Visibility       ItemEmptyImgVis;
            Visibility       ItemFileIconVis;
            bool             IsRecentsListEmpty = true;

            foreach (var entry in mostRecentlyUsed.Entries)
            {
                try
                {
                    var item = await mostRecentlyUsed.GetItemAsync(entry.Token);

                    if (item.IsOfType(StorageItemTypes.File))
                    {
                        IsRecentsListEmpty = false;
                    }
                }
                catch (Exception) { }
            }

            if (IsRecentsListEmpty)
            {
                Empty.Visibility = Visibility.Visible;
            }
            else
            {
                Empty.Visibility = Visibility.Collapsed;
            }

            foreach (Windows.Storage.AccessCache.AccessListEntry entry in mostRecentlyUsed.Entries)
            {
                string mruToken = entry.Token;
                try
                {
                    IStorageItem item = await mostRecentlyUsed.GetItemAsync(mruToken);

                    if (item.IsOfType(StorageItemTypes.File))
                    {
                        ItemName  = item.Name;
                        ItemPath  = item.Path;
                        ItemType  = StorageItemTypes.File;
                        ItemImage = new BitmapImage();
                        StorageFile file = await StorageFile.GetFileFromPathAsync(ItemPath);

                        var thumbnail = await file.GetThumbnailAsync(Windows.Storage.FileProperties.ThumbnailMode.SingleItem, 30, Windows.Storage.FileProperties.ThumbnailOptions.UseCurrentScale);

                        if (thumbnail == null)
                        {
                            ItemEmptyImgVis = Visibility.Visible;
                        }
                        else
                        {
                            await ItemImage.SetSourceAsync(thumbnail.CloneStream());

                            ItemEmptyImgVis = Visibility.Collapsed;
                        }
                        ItemFolderImgVis = Visibility.Collapsed;
                        ItemFileIconVis  = Visibility.Visible;
                        recentItemsCollection.Add(new RecentItem()
                        {
                            RecentPath = ItemPath, Name = ItemName, Type = ItemType, FolderImg = ItemFolderImgVis, EmptyImgVis = ItemEmptyImgVis, FileImg = ItemImage, FileIconVis = ItemFileIconVis
                        });
                    }
                }
                catch (FileNotFoundException)
                {
                    mostRecentlyUsed.Remove(mruToken);
                }
                catch (UnauthorizedAccessException)
                {
                    // Skip item until consent is provided
                }
                catch (COMException ex)
                {
                    mostRecentlyUsed.Remove(mruToken);
                    System.Diagnostics.Debug.WriteLine(ex);
                }
            }

            if (recentItemsCollection.Count == 0)
            {
                Empty.Visibility = Visibility.Visible;
            }
        }