using Prism.Commands; using Prism.Mvvm; using Prism.Services.Dialogs; using System; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using TeamAAS_VP.Interfaces; using TeamAAS_VP.Models; using TeamAAS_VP.Models.Lights; using TeamAAS_VP.Models.PLC; using TeamAAS_VP.Resources.Languages; namespace TeamAAS_VP.ViewModels.Product { public class LightChannelListViewModel : BindableBase, IDialogAware { private readonly IConfigService _configService; private ObservableCollection _AllLightChannels; public ObservableCollection AllLightChannels { get { return _AllLightChannels; } set { SetProperty(ref _AllLightChannels, value); } } private DelegateCommand _ConfirmCommand; public DelegateCommand ConfirmCommand => _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand)); private DelegateCommand _CancelCommand; public DelegateCommand CancelCommand => _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand)); public LightChannelListViewModel(IConfigService configService) { _configService = configService; } void ExecuteConfirmCommand() { //获取所有选择的通道 var selectedChannels = AllLightChannels.Where(c => c.IsSelected).ToArray(); //将通道配置扩展转换为通道配置 ChannelConfig[] Channels = selectedChannels.Select(c => c.ToChannelConfig()).ToArray(); IDialogParameters parameters = new DialogParameters(); parameters.Add("Channels", Channels); RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters)); } /// /// 取消按钮 /// void ExecuteCancelCommand() { RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, null)); } public string Title { get; set; } = "添加机器人"; public event Action RequestClose; public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { Title = "选择要使用的光源通道"; ChannelConfig[] channels=parameters.GetValue("Channels"); AllLightChannels =new ObservableCollection(); var allChanels = _configService.GetAllLightChannels(); foreach (var item in allChanels) { AllLightChannels.Add(new ChannelConfigEx(item)); } //设置已选择的通道 foreach (var channel in channels) { var exist = AllLightChannels.FirstOrDefault(c => c.GlobalIndex == channel.GlobalIndex); if (exist != null) { exist.IsSelected = true; } } } } }