//Обновление датагрида (избегать излишних вызовов, занимает файлы секунды на 2) public void RefreshDataGrid() { try { outputDevice.Stop(); Thread.Sleep(100); mainReader = null; //resetting datagrid and list of tracks tracksDataGrid.ItemsSource = null; tracksList.Clear(); //for each file that satisfies set mask foreach (string file in System.IO.Directory.GetFiles(workingFolderPath).Where(file => allowedExtensions.Any(file.ToLower().EndsWith))) { //Debug.WriteLine("filename = " + file); try { var newTrack = new Track //create new track based on file { name = System.IO.Path.GetFileName(file), //name of track with extension duration = GetAudioLength(file), //audiofile length in string format mm:ss extension = System.IO.Path.GetExtension(file), //extension of file filePath = file, //path to file audioTimeSpanLength = GetAudioTimeSpanLength(file) //audio length in seconds (TimeSpan) }; tracksList.Add(newTrack); } catch (IOException ex) { Debug.WriteLine("\n" + ex + "\n"); string caption = "Файл повреждён"; string message = "Файл " + System.IO.Path.GetFileName(file) + " повреждён и не может быть воспроизведён, удалить?"; System.Windows.MessageBoxButton buttons = System.Windows.MessageBoxButton.YesNo; if (System.Windows.MessageBox.Show(message, caption, buttons) == MessageBoxResult.Yes) { File.Delete(file); } continue; } } tracksDataGrid.ItemsSource = tracksList; //setting ItemSource automatically rerenders datagrid if (tracksDataGrid.Items.Count > 0) { tracksDataGrid.SelectedIndex = 0; //default selected track on startup is first } } catch (Exception ex) { Debug.WriteLine("\n" + ex + "\n"); } }
//Добавление файла private void contentControlAddTrack_MouseUp(object sender, MouseButtonEventArgs e) { try { OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Выбор файлов"; ofd.Filter = "Audio files|*.mp3;*.wav"; ofd.FilterIndex = 0; ofd.InitialDirectory = workingFolderPath; ofd.Multiselect = true; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { var selectedItem = tracksDataGrid.SelectedItem; foreach (string file in ofd.FileNames) { try { File.Copy(file, workingFolderPath + @"\" + System.IO.Path.GetFileName(file)); var newTrack = new Track //create new track based on file { name = System.IO.Path.GetFileName(file), //name of track with extension duration = GetAudioLength(file), //audiofile length in string format mm:ss extension = System.IO.Path.GetExtension(file), //extension of file filePath = workingFolderPath + @"\" + System.IO.Path.GetFileName(file), //path to file audioTimeSpanLength = GetAudioTimeSpanLength(file) //audio length in seconds (TimeSpan) }; tracksList.Add(newTrack); } catch (Exception ex) //continues through selected files if file cannot be copied { Debug.WriteLine("\nИСКЛЮЧЕНИЕ В ЦИКЛЕ: " + ex + "\n"); continue; } } fileJustAdded = true; tracksDataGrid.ItemsSource = null; tracksDataGrid.ItemsSource = tracksList; tracksDataGrid.SelectedItem = selectedItem; } } catch (Exception ex) { Debug.WriteLine("\n" + ex + "\n"); } }