微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从文件加载轮播图像

如何解决从文件加载轮播图像

我有一个带有 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 举报,一经查实,本站将立刻删除。