StatusBarViewModel.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using CommunityToolkit.Mvvm.Input;
  3. using PLCIOPointTool.Services;
  4. namespace PLCIOPointTool.ViewModels;
  5. public partial class StatusBarViewModel : ObservableObject, IDisposable
  6. {
  7. private readonly ICommunicator _communicator;
  8. private bool disposedValue;
  9. [ObservableProperty]
  10. private string _netID;
  11. [ObservableProperty]
  12. private int _port;
  13. [ObservableProperty]
  14. private bool _isInputEnable;
  15. [ObservableProperty]
  16. private string _buttonContent;
  17. [ObservableProperty]
  18. [NotifyCanExecuteChangedFor(nameof(ConnectOrDisconnectCommand))]
  19. private bool _isNotInOperating;
  20. [ObservableProperty]
  21. private string _status;
  22. public StatusBarViewModel(ICommunicator communicator)
  23. {
  24. _netID = string.Empty;
  25. _port = 531;
  26. _isInputEnable = true;
  27. _buttonContent = "Connect";
  28. _isNotInOperating = true;
  29. _status = "Disconnected";
  30. _communicator = communicator;
  31. _communicator.ConnectionStatusChanged += OnConnectionStatusChanged;
  32. }
  33. [RelayCommand(CanExecute = nameof(IsNotInOperating))]
  34. private void ConnectOrDisconnect()
  35. {
  36. IsNotInOperating = false;
  37. Task.Run(() =>
  38. {
  39. switch (ButtonContent)
  40. {
  41. case "Connect":
  42. _communicator.Open(NetID, Port);
  43. break;
  44. case "DisConnect":
  45. _communicator.Close();
  46. break;
  47. default:
  48. break;
  49. }
  50. App.Current.Dispatcher.BeginInvoke(() =>
  51. {
  52. IsNotInOperating = true;
  53. });
  54. });
  55. }
  56. private void OnConnectionStatusChanged(object? sender, bool e)
  57. {
  58. App.Current.Dispatcher.Invoke(() =>
  59. {
  60. ButtonContent = e ? "Disconnect" : "Connect";
  61. IsInputEnable = !e;
  62. Status = e ? "Connected" : "Disconnected";
  63. });
  64. }
  65. protected virtual void Dispose(bool disposing)
  66. {
  67. if (!disposedValue)
  68. {
  69. if (disposing)
  70. {
  71. // TODO: dispose managed state (managed objects)
  72. _communicator.ConnectionStatusChanged -= OnConnectionStatusChanged;
  73. }
  74. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  75. // TODO: set large fields to null
  76. disposedValue = true;
  77. }
  78. }
  79. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  80. // ~StatusBarViewModel()
  81. // {
  82. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  83. // Dispose(disposing: false);
  84. // }
  85. public void Dispose()
  86. {
  87. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  88. Dispose(disposing: true);
  89. GC.SuppressFinalize(this);
  90. }
  91. }