[UWP][C#]Loading Carousel Image from File

Rose 1 Reputation point
2021-03-16T03:19:13.347+00:00

I am have a pivot with 2 carousels showing the picture from files.
Code:

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 buku = await library.CreateFolderAsync("buku", 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];
            }
    }

I want if the image has not finished loading and another pivot clicked, then the previous image does not load and is displayed in another carousel files (only displays the carousel files of the selected pivot). For example, by default it shows the pivot of "semua buku" and when I click on the pivot of "kurikulum pjj", it will display the carousel "pjjcarousel" with its files only.

Universal Windows Platform (UWP)
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,600 questions
{count} votes