public Task LoadMusicList() { return(Task.Run(() => { //List<MusicsGroup> musicGroups = new List<MusicsGroup>(); //读取配置文件中歌曲列表信息 ConfigManager.Instance.Load(); ConfigManager.Instance.Config?.MusicList?.ForEach(musicLists => { if (musicLists != null) { var musicGroup = new MusicsGroup() { IsDefault = musicLists.IsDefault, GroupName = musicLists.GroupName, MusicInfos = new ObservableCollection <MusicInfoViewModel>() }; musicLists.MusicInfos.ForEach(musicInfo => { musicGroup.MusicInfos.Add(musicInfo.ToVM()); }); App.Current.Dispatcher.BeginInvoke(new Action(() => { _musicList.Add(musicGroup); })); } }); })); }
public Task AddMusicList(string strGroupName, bool isDefault, List <string> pathList) { return(Task.Run(() => { if (ConfigManager.Instance.Config?.MusicList == null) { return; } var vmGroup = _musicList.ToList().Where(g => isDefault ? g.IsDefault : g.GroupName.Equals(strGroupName)).FirstOrDefault(); var group = ConfigManager.Instance.Config?.MusicList?.Where(g => isDefault ? g.IsDefault : g.GroupName.Equals(strGroupName)).FirstOrDefault(); Debug.Assert((group == null && vmGroup == null) || (group != null && vmGroup != null)); if (vmGroup == null) { vmGroup = new MusicsGroup() { GroupName = strGroupName, IsDefault = isDefault, }; App.Current.Dispatcher.Invoke(() => { _musicList.Add(vmGroup); }); } if (group != null) { //在现有组上增加 if (group.MusicInfos == null) { group.MusicInfos = new List <MusicInfo>(); } } else { //新增分组 group = new MusicDirectoryInfo() { GroupName = strGroupName, IsDefault = isDefault, MusicInfos = new List <MusicInfo>() }; ConfigManager.Instance.Config.MusicList.Add(group); } pathList.ForEach(path => { var musicInfo = MusicPlayer.GetMusicInfo(path); if (musicInfo != null) { group.MusicInfos.Add(musicInfo); //增加到viewmodel App.Current.Dispatcher.BeginInvoke(new Action(() => { vmGroup.MusicInfos.Add(musicInfo.ToVM()); })); } }); })); }