EditDeviceViewModel.cs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. using Mapster;
  2. namespace DeviceManagement.ViewModels.Dialog;
  3. internal partial class EditDeviceViewModel(DeviceCollection deviceCollection) : ObservableObject, IDialogAware
  4. {
  5. public DialogCloseListener RequestClose { get; set; }
  6. [ObservableProperty]
  7. private DeviceInfo_VM? _TempDevice;
  8. [ObservableProperty]
  9. private string? _Title;
  10. public bool CanCloseDialog()
  11. {
  12. return true;
  13. }
  14. public void OnDialogClosed()
  15. {
  16. }
  17. public void OnDialogOpened(IDialogParameters parameters)
  18. {
  19. this.Title = "设备编辑";
  20. if (!parameters.TryGetValue<DeviceInfo_VM>("Device", out DeviceInfo_VM? device) || device is null)
  21. {
  22. this.RequestClose.Invoke();
  23. return;
  24. }
  25. this.TempDevice = new();
  26. device.Adapt(this.TempDevice);
  27. }
  28. [RelayCommand]
  29. private void SaveDevice()
  30. {
  31. if (this.TempDevice is null||this.TempDevice.Guid is null)
  32. {
  33. MessageBox.Show("Error");
  34. return;
  35. }
  36. if (!deviceCollection.DeviceList.TryGetValue(this.TempDevice.Guid.Value, out DeviceInfo_VM? device) || device is null)
  37. {
  38. MessageBox.Show("Error");
  39. return;
  40. }
  41. this.TempDevice.Adapt(device);
  42. MessageBox.Show("设备信息修改成功!", "设备信息", MessageBoxButton.OK, MessageBoxImage.Information);
  43. this.RequestClose.Invoke();
  44. }
  45. [RelayCommand]
  46. private void Close()
  47. {
  48. this.RequestClose.Invoke();
  49. }
  50. }