AddRobotViewModel.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using Prism.Commands;
  2. using Prism.Ioc;
  3. using Prism.Mvvm;
  4. using Prism.Services.Dialogs;
  5. using System;
  6. using System.Collections.Generic;
  7. using System.Linq;
  8. using System.Windows;
  9. using TeamAAS_VP.Core;
  10. using TeamAAS_VP.Models;
  11. using TeamAAS_VP.Resources.Languages;
  12. using TouchSocket.Core;
  13. namespace TeamAAS_VP.ViewModels.Setting
  14. {
  15. public class AddRobotViewModel : BindableBase, IDialogAware
  16. {
  17. private RobotInfo _Robot;
  18. /// <summary>
  19. /// 机器人
  20. /// </summary>
  21. public RobotInfo Robot
  22. {
  23. get { return _Robot; }
  24. set { SetProperty(ref _Robot, value); }
  25. }
  26. private PlcInfo _SelectedPlc;
  27. public PlcInfo SelectedPlc
  28. {
  29. get { return _SelectedPlc; }
  30. set
  31. {
  32. SetProperty(ref _SelectedPlc, value);
  33. if (value != null)
  34. {
  35. Robot.PLC = value.Id;
  36. }
  37. else
  38. {
  39. Robot.PLC = Guid.Empty;
  40. }
  41. }
  42. }
  43. private DelegateCommand _ConfirmCommand;
  44. public DelegateCommand ConfirmCommand =>
  45. _ConfirmCommand ?? (_ConfirmCommand = new DelegateCommand(ExecuteConfirmCommand, CanExecuteConfirmCommand).ObservesProperty(()=> Robot.RobotName));
  46. private DelegateCommand _CancelCommand;
  47. public DelegateCommand CancelCommand =>
  48. _CancelCommand ?? (_CancelCommand = new DelegateCommand(ExecuteCancelCommand));
  49. public AddRobotViewModel()
  50. {
  51. }
  52. /// <summary>
  53. /// 确认按钮
  54. /// </summary>
  55. /// <returns></returns>
  56. bool CanExecuteConfirmCommand()
  57. {
  58. return Robot!=null && Robot.RobotName!=null && !string.IsNullOrEmpty(Robot.RobotName.Trim());
  59. }
  60. void ExecuteConfirmCommand()
  61. {
  62. if (string.IsNullOrEmpty(Robot.RobotName))
  63. {
  64. return;
  65. }
  66. if (!FileHelper.CheckFileName(Robot.RobotName))
  67. {
  68. MessageBox.Show(Lang.AddCameraVM_message1, "AAS", MessageBoxButton.OK, MessageBoxImage.Error);
  69. return;
  70. }
  71. IDialogParameters parameters = new DialogParameters();
  72. parameters.Add("Robot", Robot);
  73. Tuple<bool, RobotInfo> rest = new Tuple<bool, RobotInfo>(false, Robot);
  74. RequestClose?.Invoke(new DialogResult(ButtonResult.OK, parameters));
  75. }
  76. /// <summary>
  77. /// 取消按钮
  78. /// </summary>
  79. void ExecuteCancelCommand()
  80. {
  81. IDialogParameters parameters = new DialogParameters();
  82. parameters.Add("Robot", Robot);
  83. Tuple<bool, RobotInfo> rest = new Tuple<bool, RobotInfo>(false, Robot);
  84. RequestClose?.Invoke(new DialogResult(ButtonResult.Cancel, parameters));
  85. }
  86. public string Title { get; set; } = "添加机器人";
  87. public event Action<IDialogResult> RequestClose;
  88. public bool CanCloseDialog()
  89. {
  90. return true;
  91. }
  92. public void OnDialogClosed()
  93. {
  94. }
  95. public void OnDialogOpened(IDialogParameters parameters)
  96. {
  97. Title = Lang.增加机器人;
  98. Robot = new RobotInfo() { Id = Guid.NewGuid(),RobotBrand= Enums.RobotBrand.Default,ConnectType= Enums.TCPConnectType.Client, Terminator= Enums.Terminator.CRLF, DataEncoding= Enums.DataEncoding.Default };
  99. }
  100. }
  101. }