CreateDeviceViewModel.cs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. namespace DeviceScanner.ViewModels.Dialogs;
  2. internal partial class CreateDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware
  3. {
  4. [ObservableProperty]
  5. private DeviceInfo_VM? _TempDevice;
  6. [ObservableProperty]
  7. private string? _Title;
  8. public DialogCloseListener RequestClose { get; set; }
  9. public bool CanCloseDialog()
  10. {
  11. return true;
  12. }
  13. public void OnDialogClosed()
  14. {
  15. }
  16. public void OnDialogOpened(IDialogParameters parameters)
  17. {
  18. this.Title = "添加设备";
  19. if (!parameters.TryGetValue<DeviceInfo_VM>("Device", out DeviceInfo_VM? device) || device is null)
  20. {
  21. this.RequestClose.Invoke();
  22. return;
  23. }
  24. this.TempDevice = device;
  25. }
  26. [RelayCommand]
  27. private void SaveDevice()
  28. {
  29. if (this.TempDevice is null)
  30. return;
  31. //if (this.TempDevice is null || this.TempDevice.Guid is null)
  32. // return;
  33. //if (!deviceCollection.DeviceList.ContainsKey(this.TempDevice.Guid.Value))
  34. //{
  35. // MessageBox.Show($"Device Name \"{this.TempDevice.Guid}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  36. // return;
  37. //}
  38. if (deviceCollection.DeviceList.Values.Any(t => t.DeviceName == this.TempDevice.DeviceName))
  39. {
  40. MessageBox.Show($"Device Name \"{this.TempDevice.DeviceName}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  41. return;
  42. }
  43. if (deviceCollection.DeviceList.Values.Any(t => t.IP == this.TempDevice.IP))
  44. {
  45. MessageBox.Show($"IP Address \"{this.TempDevice.IP}\" already exist", "Warning", MessageBoxButton.OK, MessageBoxImage.Error);
  46. return;
  47. }
  48. if (string.IsNullOrEmpty(this.TempDevice.DeviceName))
  49. {
  50. MessageBox.Show("设备未命名", "Warning", MessageBoxButton.OK, MessageBoxImage.Warning);
  51. return;
  52. }
  53. MessageBoxResult result = MessageBox.Show($"确认添加 {this.TempDevice.DeviceModel}-{this.TempDevice.DeviceName} 设备?", "添加设备", MessageBoxButton.YesNo, MessageBoxImage.Question);
  54. if (result != MessageBoxResult.Yes)
  55. return;
  56. MessageBox.Show("设备添加成功!", "Info", MessageBoxButton.OK, MessageBoxImage.Information);
  57. this.TempDevice.Guid = Guid.NewGuid();
  58. deviceCollection.DeviceList.Add(this.TempDevice.Guid.Value, this.TempDevice);
  59. this.RequestClose.Invoke();
  60. }
  61. [RelayCommand]
  62. private void Close()
  63. {
  64. this.RequestClose.Invoke();
  65. }
  66. }