EditDeviceViewModel.cs 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. namespace DeviceManagement.ViewModels.Dialog;
  2. internal partial class EditDeviceViewModel(DeviceCollection deviceCollection, IUICaller uICaller) : ObservableObject, IDialogAware
  3. {
  4. public DialogCloseListener RequestClose { get; set; }
  5. [ObservableProperty]
  6. private DeviceInfo_VM? _TempDevice;
  7. [ObservableProperty]
  8. private string? _Title;
  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 = new();
  25. device.Adapt(this.TempDevice);
  26. }
  27. [RelayCommand]
  28. private void SaveDevice()
  29. {
  30. if (this.TempDevice is null || this.TempDevice.Guid is null)
  31. {
  32. MessageBox.Show("Error");
  33. return;
  34. }
  35. if (!deviceCollection.DeviceList.TryGetValue(this.TempDevice.Guid.Value, out DeviceInfo_VM? device) || device is null)
  36. {
  37. MessageBox.Show("Error");
  38. return;
  39. }
  40. if (!uICaller.UpdateDeviceInfo(this.TempDevice.Adapt<DeviceInfo>()).Result)
  41. {
  42. MessageBox.Show("Device Offline");
  43. return;
  44. }
  45. this.TempDevice.Adapt(device);
  46. MessageBox.Show("设备信息修改成功!", "设备信息", MessageBoxButton.OK, MessageBoxImage.Information);
  47. this.RequestClose.Invoke();
  48. }
  49. [RelayCommand]
  50. private void Close()
  51. {
  52. this.RequestClose.Invoke();
  53. }
  54. }