public ChannelsViewModel() { System.Diagnostics.Debug.WriteLine("<ChannelsViewModel> New Instance"); // 将节目列表进行分组 Programs = new CollectionViewSource(); var currentPrograms = TelevisionService.Instance.AvaliablePrograms; var channelAdapters = new List <ChannelTypeAdapter>(); foreach (var group in currentPrograms.GroupBy(program => program.ProgramInfo.Channel.Type)) { // TODO: 进一步完善分组 var adapter = new ChannelTypeAdapter(); adapter.AddRange(group); string typeName = ""; if (group.Key.HasFlag(ChannelType.Central)) { typeName = "央视"; } else if (group.Key.HasFlag(ChannelType.Beijing)) { typeName = "北京"; } else if (group.Key.HasFlag(ChannelType.Local)) { typeName = "地方"; } else if (group.Key.HasFlag(ChannelType.Special)) { typeName = "特别"; } else if (group.Key.HasFlag(ChannelType.Radio)) { typeName = "广播"; } if (group.Key.HasFlag(ChannelType.Hd)) { typeName += "高清"; } else if (group.Key.HasFlag(ChannelType.Standard)) { typeName += "标清"; } adapter.Type = typeName + "频道"; channelAdapters.Add(adapter); } Programs.Source = channelAdapters; Programs.IsSourceGrouped = true; // 新建Command的实例 LoveCurrentChannel = new DelegateCommand(() => { if (IsCurrentChannelFavourite) { CollectionService.Instance.RemoveChannel(SelectedProgram.ProgramInfo.Channel); } else { CollectionService.Instance.AddChannel(SelectedProgram.ProgramInfo.Channel); } }, () => SelectedProgram != null); LoveCurrentProgram = new DelegateCommand(() => { if (IsCurrentProgramFavourite) { CollectionService.Instance.RemoveProgram(SelectedProgram.ProgramInfo); } else { CollectionService.Instance.AddProgram(SelectedProgram.ProgramInfo); } }, () => SelectedProgram != null); // 监听收藏的变动 CollectionService.Instance.ChannelListChanged += (sender, e) => { if (SelectedProgram != null) { IsCurrentChannelFavourite = CollectionService.Instance.CheckChannel(SelectedProgram.ProgramInfo.Channel); } }; CollectionService.Instance.ProgramListChanged += (sender, e) => { if (SelectedProgram != null) { IsCurrentProgramFavourite = CollectionService.Instance.CheckProgram(SelectedProgram.ProgramInfo); } }; // 绑定功能按钮的Command ToggleSidePanel = new DelegateCommand(() => { VisualStateManager.GoToState(Host, _IsSidePanelExpaneded ? "SideCollapsed" : "SideExpanded", true); _IsSidePanelExpaneded = !_IsSidePanelExpaneded; }, () => SelectedProgram != null); }
public ChannelsViewModel() { // 提供最新实例的访问 Instance = this; // 将节目列表进行分组 Programs = new CollectionViewSource(); var currentPrograms = TelevisionService.Instance.AvaliablePrograms; var channelAdapters = new List <ChannelTypeAdapter>(); foreach (var group in currentPrograms.GroupBy(program => program.ProgramInfo.Channel.Type)) { // TODO: 进一步完善分组 var adapter = new ChannelTypeAdapter(); adapter.AddRange(group); string typeName = ""; if (group.Key.HasFlag(ChannelType.Central)) { typeName = "央视"; } else if (group.Key.HasFlag(ChannelType.Beijing)) { typeName = "北京"; } else if (group.Key.HasFlag(ChannelType.Local)) { typeName = "地方"; } else if (group.Key.HasFlag(ChannelType.Special)) { typeName = "特别"; } else if (group.Key.HasFlag(ChannelType.Radio)) { typeName = "广播"; } if (group.Key.HasFlag(ChannelType.Hd)) { typeName += "高清"; } else if (group.Key.HasFlag(ChannelType.Standard)) { typeName += "标清"; } adapter.Type = typeName + "频道"; channelAdapters.Add(adapter); } Programs.Source = channelAdapters; Programs.IsSourceGrouped = true; AddChannelRecordTask = new DelegateCommand(async() => { await new RecordDialog(SelectedProgram.ProgramInfo.Channel, SelectedSource).ShowAsync(); }); // 应该不存在无法执行的情况,就不执行检查了 // 监听收藏的变动 CollectionService.Instance.ChannelListChanged += (sender, e) => { if (SelectedProgram != null) { IsCurrentChannelFavourite = CollectionService.Instance.CheckChannel(SelectedProgram.ProgramInfo.Channel); } }; CollectionService.Instance.ProgramListChanged += (sender, e) => { if (SelectedProgram != null) { IsCurrentProgramFavourite = CollectionService.Instance.CheckProgram(SelectedProgram.ProgramInfo); } }; // 绑定功能按钮的Command ToggleSidePanel = new DelegateCommand(() => { VisualStateManager.GoToState(Host, _IsSidePanelExpaneded ? "SideCollapsed" : "SideExpanded", true); _IsSidePanelExpaneded = !_IsSidePanelExpaneded; }, () => SelectedProgram != null); }