public void Drop(IDropInfo dropInfo) { var files = (dropInfo.Data as DataObject)?.GetFileDropList().Cast <string>(); dropInfo.Effects = DragDropEffects.None; if (files != null) { var mp3s = files.Where(x => Path.GetExtension(x).Equals(".mp3")); if (mp3s.Any()) { var songViewModels = this.GenerateSongViewModels(mp3s); // Do not overwrite any existing / already loaded instances. if (songViewModels.Any() && DisplayedSongs == null) { DisplayedSongs = new ObservableCollection <SongViewModel>(); } // Convert the file paths into view models like the "normal" // loading does as well. foreach (var song in songViewModels) { DisplayedSongs.Add(song); this.songs.Add(song); } this.UpdateSongIndices(); dropInfo.Effects = DragDropEffects.Copy; } } }
private async Task <bool> LoadSongsAsync(OpenFileDialog dialogSettings) { var success = dialogSettings.ShowDialog() ?? false; if (success) { var message = "Loading songs..."; var files = dialogSettings.FileNames; var fileCount = files.Count(); var songViewModels = this.GenerateSongViewModels(files); var enumerator = songViewModels.GetEnumerator(); var uiDispatcher = Dispatcher.CurrentDispatcher; var controller = await this.dialogCoordinator.ShowProgressAsync(this, "Load Songs", message, false); controller.Maximum = fileCount; controller.Minimum = 0; // Do not overwrite any existing / already loaded instances. if (songViewModels.Any() && DisplayedSongs == null) { DisplayedSongs = new ObservableCollection <SongViewModel>(); } // A separate Task is needed in order for the progress bar content // to be drawn. Otherwise only a blank, grey overlay is shown. await Task.Run(() => { for (int i = 0; i < fileCount; i++) { enumerator.MoveNext(); var viewModel = enumerator.Current; var currentCount = i + 1; uiDispatcher.Invoke(() => { DisplayedSongs.Add(viewModel); }); this.songs.Add(viewModel); controller.SetProgress(currentCount); controller.SetMessage(String.Format("{0} ({1}/{2})", message, currentCount, fileCount)); } }); await controller.CloseAsync(); } return(success); }