OutputBarViewModel.cs 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using CommunityToolkit.Mvvm.ComponentModel;
  2. using PLCIOPointTool.Services;
  3. using System.Collections.ObjectModel;
  4. namespace PLCIOPointTool.ViewModels;
  5. public partial class OutputBarViewModel : ObservableObject, IDisposable
  6. {
  7. private readonly IOutputService _outputService;
  8. private bool disposedValue;
  9. [ObservableProperty]
  10. private ObservableCollection<string> _outputs;
  11. public OutputBarViewModel(IOutputService outputService)
  12. {
  13. _outputService = outputService;
  14. _outputService.MessageReceived += OnMessageReceived;
  15. _outputs = [];
  16. }
  17. private void OnMessageReceived(object? sender, string e)
  18. {
  19. App.Current.Dispatcher.BeginInvoke(() =>
  20. {
  21. if (Outputs.Count >= 50)
  22. {
  23. Outputs.RemoveAt(Outputs.Count - 1);
  24. }
  25. Outputs.Insert(0, e);
  26. });
  27. }
  28. protected virtual void Dispose(bool disposing)
  29. {
  30. if (!disposedValue)
  31. {
  32. if (disposing)
  33. {
  34. // TODO: dispose managed state (managed objects)
  35. _outputService.MessageReceived -= OnMessageReceived;
  36. Outputs.Clear();
  37. }
  38. // TODO: free unmanaged resources (unmanaged objects) and override finalizer
  39. // TODO: set large fields to null
  40. disposedValue = true;
  41. }
  42. }
  43. // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources
  44. // ~OutputBarViewModel()
  45. // {
  46. // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  47. // Dispose(disposing: false);
  48. // }
  49. public void Dispose()
  50. {
  51. // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method
  52. Dispose(disposing: true);
  53. GC.SuppressFinalize(this);
  54. }
  55. }