namespace DeviceScanner.ViewModels.Dialogs; internal partial class CreateDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware { [ObservableProperty] private DeviceInfo_VM? _TempDevice; [ObservableProperty] private string? _Title; public DialogCloseListener RequestClose { get; set; } public bool CanCloseDialog() { return true; } public void OnDialogClosed() { } public void OnDialogOpened(IDialogParameters parameters) { this.Title = "添加设备"; if (!parameters.TryGetValue("Device", out DeviceInfo_VM? device) || device is null) { this.RequestClose.Invoke(); return; } this.TempDevice = device; } [RelayCommand] private void SaveDevice() { if (this.TempDevice is null) return; //if (this.TempDevice is null || this.TempDevice.Guid is null) // return; //if (!deviceCollection.DeviceList.ContainsKey(this.TempDevice.Guid.Value)) //{ // MessageBox.Show($"Device Name \"{this.TempDevice.Guid}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error); // return; //} if (deviceCollection.DeviceList.Values.Any(t => t.DeviceName == this.TempDevice.DeviceName)) { MessageBox.Show($"Device Name \"{this.TempDevice.DeviceName}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (deviceCollection.DeviceList.Values.Any(t => t.IP == this.TempDevice.IP)) { MessageBox.Show($"IP Address \"{this.TempDevice.IP}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error); return; } if (string.IsNullOrEmpty(this.TempDevice.DeviceName)) { MessageBox.Show("设备未命名", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning); return; } MessageBoxResult result = MessageBox.Show($"确认添加 {this.TempDevice.DeviceModel}-{this.TempDevice.DeviceName} 设备?", "添加设备", MessageBoxButton.YesNo, MessageBoxImage.Question); if (result != MessageBoxResult.Yes) return; MessageBox.Show("设备添加成功!", "Info", MessageBoxButton.OK, MessageBoxImage.Information); this.TempDevice.Guid = Guid.NewGuid(); deviceCollection.DeviceList.Add(this.TempDevice.Guid.Value, this.TempDevice); this.RequestClose.Invoke(); } [RelayCommand] private void Close() { this.RequestClose.Invoke(); } }