public MainViewModel() { if (IsInDesignMode) { // Code runs in Blend --> create design time data. Title = _title; NeteaseUrl = "http://music.163.com/#/my/m/music/playlist?id=6435531"; SongCollection.Add(new Song() { Title = new Title("Clover Heart's"), Artists = new Artists(new string[] { "榊原ゆい", "榊原ゆい" }), Album = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"), }); SongCollection.Add(new Song() { Title = new Title("Clover Heart's"), Artists = new Artists(new string[] { "榊原ゆい" }), Album = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"), }); SongCollection.Add(new Song() { Title = new Title("Clover Heart's"), Artists = new Artists(new string[] { "榊原ゆい" }), Album = new Album("Boommmmm", "http://p4.music.126.net/n189nEFRefNaucKD8akNQw==/7886796906449604.jpg"), }); SelectedSong = SongCollection.First(); } else { // Code runs "for real" Title = _title; NeteaseUrl = "http://music.163.com/playlist?id=6435531"; Progress = 0; audioPlayback = new AudioPlayback(); audioPlayback.Volume = Volume; audioPlayback.EndCallback += (handle, channel, data, user) => { int index = -1; if ((index = SongCollection.IndexOf(CurrentPlaySong)) != -1) { ListenCommand.Execute(SongCollection.ElementAt((index + 1) % SongCollection.Count)); } else { audioPlayback.Stop(); NowPlaying = ""; CurrentPlaySong.PlayProgress = 0; CurrentPlaySong.PlayStatus = PlayStatus.Play; timer.Enabled = false; timer.Stop(); } }; timer.Tick += (sender, args) => { Title = string.Format("{0}/{1} - {2}", audioPlayback.CurrentLength, audioPlayback.TotalLength, _title); RaisePropertyChanged("Title"); CurrentPlaySong.PlayProgress = audioPlayback.Progress; }; GetSongsCommand = new RelayCommand(async() => { if (string.IsNullOrWhiteSpace(NeteaseUrl)) { return; } var id = ""; var urlType = ""; var reg = new Regex(@".*/(.*?)\?id=(\d*)").Match(NeteaseUrl); if (reg.Success) { urlType = reg.Groups[1].Value; id = reg.Groups[2].Value; SongCollection.Clear(); switch (urlType) { case "album": foreach (var song in await NeteaseUtil.GetSongsFromAlbum(id)) { SongCollection.Add(song); RaisePropertyChanged("TotalCount"); } break; case "artist": foreach (var song in await NeteaseUtil.GetSongsFromArtist(id)) { SongCollection.Add(song); RaisePropertyChanged("TotalCount"); } break; case "playlist": foreach (var song in await NeteaseUtil.GetSongsFromPlaylist(id)) { SongCollection.Add(song); RaisePropertyChanged("TotalCount"); } break; case "song": foreach (var song in await NeteaseUtil.GetSongDetail(id)) { SongCollection.Add(song); RaisePropertyChanged("TotalCount"); } break; } } else { SongCollection.Clear(); foreach (var song in await NeteaseUtil.SearchSongs(NeteaseUrl)) { SongCollection.Add(song); RaisePropertyChanged("TotalCount"); } } }); PlaylistDownloadCommand = new RelayCommand <Song>((song) => { SelectedSong = song; if (string.IsNullOrWhiteSpace(SongTrackUrl)) { return; } var downloader = new DownloadUtils(); downloader.DownloadProgressChanged += (sender, args) => { song.DownProgress = args.ProgressPercentage; BytesReceived = args.BytesReceived.ToString(); TotalBytesToReceive = args.TotalBytesToReceive.ToString(); }; downloader.DownloadFileCompleted += (sender, args) => { if (DownloadNext) { int index = -1; if ((index = SongCollection.IndexOf(song)) != -1 && index + 1 < SongCollection.Count) { PlaylistDownloadCommand.Execute(SongCollection.ElementAt(index + 1)); } } else { BytesReceived = "0"; TotalBytesToReceive = "0"; } }; downloader.Get(SongTrackUrl, Path.Combine("music", FileUtils.GetSafeFileName(song.SongFileName))); }); ListenCommand = new RelayCommand <Song>((song) => { SelectedSong = song; if (string.IsNullOrWhiteSpace(SongTrackUrl)) { return; } if (song.PlayStatus == PlayStatus.Play) { if (CurrentPlaySong != null && CurrentPlaySong != song) { audioPlayback.Stop(); CurrentPlaySong.PlayProgress = 0; CurrentPlaySong.PlayStatus = PlayStatus.Play; } audioPlayback.Load(SongTrackUrl); audioPlayback.Play(); CurrentPlaySong = song; NowPlaying = string.Format("Now Playing {0} - {1}", song.Artists, song.Title); timer.Enabled = true; timer.Start(); song.PlayProgress = 0; song.PlayStatus = PlayStatus.Stop; } else { audioPlayback.Stop(); NowPlaying = ""; song.PlayStatus = PlayStatus.Play; song.PlayProgress = 0; timer.Enabled = false; timer.Stop(); } }); OpenUrlCommand = new RelayCommand <string>((link) => { System.Diagnostics.Process.Start(link); }); CopyUrlCommand = new RelayCommand <string>((link) => { Clipboard.SetText(link); }); WindowClosing = new RelayCommand(() => { Properties.Settings.Default.Save(); }); } }