private void LoadImageFiles(string directoryPath, string selectedImage) { var imageFiles = recognisedImageExtensions.SelectMany(pattern => new DirectoryInfo(directoryPath).GetFiles(pattern, SearchOption.TopDirectoryOnly).Where(it => (it.Attributes & FileAttributes.Hidden) == 0)).Distinct().ToArray(); var images = new List <IImagePreviewItemViewModel>(); foreach (var file in FileExplorerSortingProvider.Sort(imageFiles, directoryPath)) { images.Add(_imagePreviewItemViewModelFactory.CreateImagePreviewItemViewModel(file.FullName, (SelectedImage) => { SelectedImageInList = SelectedImage; })); } Images = new ObservableCollection <IImagePreviewItemViewModel>(images); OnPropertyChanged(nameof(Images)); if (Images.Count > 0) { if (string.IsNullOrEmpty(selectedImage)) { SelectedImage = Images.First(); } else { SelectedImage = Images.FirstOrDefault(it => Path.Equals(it.Path, selectedImage)); // something else then image was selected if (SelectedImage == null) { SelectedImage = Images.First(); } } SetNavigationButtonsVisibility(); _selectedImageInList = SelectedImage; OnPropertyChanged(nameof(SelectedImageInList)); } else { SelectedImage = null; } }
public ImagePreviewViewModel(IThrottledActionInvokerFactory throttledActionInvokerFactory, IImagePreviewItemViewModelFactory imagePreviewItemViewModelFactory, IUserSettings userSettings) { _throttledActionInvokerFactory = throttledActionInvokerFactory; _imagePreviewItemViewModelFactory = imagePreviewItemViewModelFactory; _userSettings = userSettings; CloseCommand = new RelayCommand(() => { Application.Current.MainWindow.Close(); }); SlideshowCommand = new RelayCommand(() => { _inSlideshowMode = true; SetSelectedImage(SelectedImageInList); }); NextImageCommand = new RelayCommand(() => { SelectedImage = Images.SkipWhile(it => it != SelectedImage).Skip(1).First(); _selectedImageInList = SelectedImage; OnPropertyChanged(nameof(SelectedImageInList)); NextImageButtonVisible = SelectedImage != Images.Last(); PreviousImageButtonVisible = SelectedImage != Images.First(); }); PreviousImageCommand = new RelayCommand(() => { SelectedImage = Images.TakeWhile(it => it != SelectedImage).Last(); _selectedImageInList = SelectedImage; OnPropertyChanged(nameof(SelectedImageInList)); PreviousImageButtonVisible = SelectedImage != Images.First(); NextImageButtonVisible = SelectedImage != Images.Last(); }); }
private void SetSelectedImage(IImagePreviewItemViewModel image) { if (_inSlideshowMode) { NextImageButtonVisible = false; PreviousImageButtonVisible = false; SelectedImage = image; _selectedImageInList = image; OnPropertyChanged(nameof(SelectedImageInList)); if (Images.IndexOf(image) < Images.Count - 1) { _slideshowScheduledActionInvoker.ScheduleAction(() => { var currentIndex = Images.IndexOf(SelectedImage); if (Images.Count > currentIndex + 1) { SetSelectedImage(Images[currentIndex + 1]); } }, _slideshowDelayInSeconds * MsPerSecond); } } }