1
\$\begingroup\$

I am currently working on a Universal Windows Platform App. It involves using a flipView to display a certain number of pictures in the user's picture library e.g less than 100 to avoid performance overhead. The code is displayed below :

// The constructor of my view model
 public PictureViewModel()
        {
            // loads the data for my flipview
            Task.Run(() => loadData());


        }

ObservableCollection<BitmapImage> pictures = new ObservableCollection<BitmapImage>();

        // the binding property 
        private ObservableCollection<BitmapImage> _pictureGallery;
        public ObservableCollection<BitmapImage> PictureGallery
        {
            get { return _pictureGallery; }
            set
            {
                if (_pictureGallery != value)
                {
                    _pictureGallery = value;
                    onPropertyChanged("PictureGallery");
                }



            }
        }

 // the data for the flipview
 public async void loadData()
        {
            StorageFolder recentPictures = KnownFolders.PicturesLibrary;
            IReadOnlyList<StorageFile> recentImagesInLibrary = await recentPictures.GetFilesAsync();
            IReadOnlyList<StorageFolder> subfolders = await recentPictures.GetFoldersAsync();
            IReadOnlyList<StorageFile> subfolderImages;
            await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal, async () =>
                             {
                                 // tranversing the pictures library current folder
                                 foreach (var item in recentImagesInLibrary)
                                 {
                                     BitmapImage BitImage = new BitmapImage();
                                     var stream = await item.OpenAsync(Windows.Storage.FileAccessMode.Read);
                                     BitImage.SetSource(stream);
                                     pictures.Add(BitImage);
                                 }

                                 foreach (var subfolderItem in subfolders)
                                 {
                                     subfolderImages = await subfolderItem.GetFilesAsync();
                                     foreach (var subfolderItemImages in subfolderImages)
                                     {
                                         BitmapImage BitImage2 = new BitmapImage();
                                         var stream2 = await subfolderItemImages.OpenAsync(Windows.Storage.FileAccessMode.Read);
                                         BitImage2.SetSource(stream2);

                                         if (pictures.Count < 80)
                                         {
                                             pictures.Add(BitImage2);
                                         }
                                         else {
                                             break;
                                         }

                                     }

                                 }
                                 PictureGallery = pictures;

                             }
            );

        }

I implemented the INotifyPropertyChanged, hence the onPropertyChanged method above, below is the implementation

public event PropertyChangedEventHandler PropertyChanged;
    private void onPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }

}

While in my xaml, the flipView looks like this

<FlipView x:Name="flipView" ItemsSource="{Binding PictureGallery}" Visibility="Visible" >
                    <FlipView.ItemTemplate>
                    <DataTemplate>
                                <Image Width="200" Height="200" Source="{Binding}" Stretch="UniformToFill" />
                    </DataTemplate>
                    </FlipView.ItemTemplate>
                </FlipView>

Although, the flipView displays the pictures but the performance is poor as I am loading the observableCollecion from the Constructor. Additionally, I try to traverse subfolders in the pictures library to get some photos. I believe there is room for improvement.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.