从文件加载轮播图像

问题描述

我有一个带有 2 个轮播的枢轴,显示文件中的图片

代码

private async void bukuKategori_SelectionChanged(object sender,SelectionChangedEventArgs e)
        {
            pivot = (PivotItem)(sender as Pivot).SelectedItem;
            switch (pivot.Header.ToString())
            {
                case "Semua Buku":
                    await this.BukuContent();
                    break;
                case "Kurikulum PJJ":
                    await this.PjjContent();
                    break;
            }
        }
private async Task BukuContent(string DownloadFileName = "test.pdf")
        {
            ObservableCollection<Book> datasource = new ObservableCollection<Book>();
            datasource.Clear();
            bukuCarousel.Visibility = Visibility.Collapsed;
            pjjCarousel.Visibility = Visibility.Collapsed;
            StorageFolder library = await installedLocation.CreateFolderAsync("library",CreationCollisionoption.OpenIfExists);
            StorageFolder buku = await library.CreateFolderAsync("buku",CreationCollisionoption.OpenIfExists);
            IReadOnlyList<StorageFolder> folderName = await buku.GetFoldersAsync();
            foreach (StorageFolder folder in folderName)
            {
                IReadOnlyList<StorageFile> files = await folder.GetFilesAsync();
                IEnumerable<Temp> sortingFiles = files.Select(x => new Temp { File = x }).ToList();
                foreach (var item in sortingFiles)
                {
                    item.LastModified = (await item.File.GetBasicPropertiesAsync()).DateModified.DateTime;
                }
                IEnumerable<StorageFile> sortedfiles = sortingFiles.OrderByDescending(x => x.LastModified).Select(x => x.File).ToList();
                StorageFolder thumbfolder = await installedLocation.CreateFolderAsync("thumb",CreationCollisionoption.OpenIfExists);
                StorageFolder bukuthumb = await thumbfolder.CreateFolderAsync("buku",CreationCollisionoption.OpenIfExists);
                IReadOnlyList<StorageFolder> folderthumbName = await bukuthumb.GetFoldersAsync();
                foreach (StorageFile file in sortedfiles)
                {
                    Book book = new Book();
                    book.Name = file.displayName.ToString();
                    foreach (StorageFolder folderthumb in folderthumbName)
                    {
                         thumbFile = await folderthumb.GetFileAsync(file.Name.ToString() + ".png");
                         BitmapImage bi = new BitmapImage();
                         bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
                         book.Image = bi;
                    }
                    datasource.Add(book);
                }
            }
            if (datasource.Count > 0)
            {
                bukuCarousel.Visibility = Visibility.Visible;
                this.bukuCarousel.ItemsSource = datasource;
                this.bukuCarousel.SelectedItem = bukuCarousel.Items[0];
            }
        }

        private async Task PjjContent(String DownloadFileName = "test.pdf")
        {
            ObservableCollection<Book> datasource = new ObservableCollection<Book>();
            datasource.Clear();
            bukuCarousel.Visibility = Visibility.Collapsed;
            pjjCarousel.Visibility = Visibility.Collapsed;
            StorageFolder library = await installedLocation.CreateFolderAsync("library",CreationCollisionoption.OpenIfExists);
            StorageFolder pjj = await buku.CreateFolderAsync("pjj",CreationCollisionoption.OpenIfExists);
            IReadOnlyList<StorageFile> files = await pjj.GetFilesAsync();
            IEnumerable<Temp> sortingFiles = files.Select(x => new Temp { File = x }).ToList();
            foreach (var item in sortingFiles)
            {
                item.LastModified = (await item.File.GetBasicPropertiesAsync()).DateModified.DateTime;
            }
            IEnumerable<StorageFile> sortedfiles = sortingFiles.OrderByDescending(x => x.LastModified).Select(x => x.File).ToList();
            StorageFolder thumbfolder = await installedLocation.CreateFolderAsync("thumb",CreationCollisionoption.OpenIfExists);
            StorageFolder bukufolder = await thumbfolder.CreateFolderAsync("buku",CreationCollisionoption.OpenIfExists);
            StorageFolder pjjthumb = await bukufolder.CreateFolderAsync("pjj",CreationCollisionoption.OpenIfExists);
            foreach (StorageFile file in sortedfiles)
            {
                Book book = new Book();
                book.Name = file.displayName.ToString();
                StorageFile thumbFile;
                bool bukuada = true;
                BasicProperties pro = await file.GetBasicPropertiesAsync();
                thumbFile = await pjjthumb.GetFileAsync(file.Name.ToString() + ".png");
                string path = pjjthumb.Path;
                string filePath = Path.Combine(path,file.Name.ToString() + ".png");
                BitmapImage bi = new BitmapImage();
                bi.SetSource(await thumbFile.OpenAsync(FileAccessMode.Read));
                book.Image = bi;
         }
        datasource.Add(book);
            if (datasource.Count > 0)
            {
                pjjCarousel.Visibility = Visibility.Visible;
                this.pjjCarousel.ItemsSource = datasource;
                this.pjjCarousel.SelectedItem = pjjCarousel.Items[0];
                deleteBtn.Visibility = Visibility.Visible;
            }
        }

我想如果图片还没有加载完成并且点击了另一个pivot,那么之前的图片不会加载并显示在另一个carousel文件中(只显示所选pivot的carousel文件)。例如,认情况下它显示“semua buku”的枢轴,当我单击“kurikulum pjj”的枢轴时,它将仅显示带有文件的轮播“pjjcarousel”。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)