using CommunityToolkit.Mvvm.ComponentModel; using PLCIOPointTool.Services; using System.Collections.ObjectModel; namespace PLCIOPointTool.ViewModels; public partial class OutputBarViewModel : ObservableObject, IDisposable { private readonly IOutputService _outputService; private bool disposedValue; [ObservableProperty] private ObservableCollection _outputs; public OutputBarViewModel(IOutputService outputService) { _outputService = outputService; _outputService.MessageReceived += OnMessageReceived; _outputs = []; } private void OnMessageReceived(object? sender, string e) { App.Current.Dispatcher.BeginInvoke(() => { if (Outputs.Count >= 50) { Outputs.RemoveAt(Outputs.Count - 1); } Outputs.Insert(0, e); }); } protected virtual void Dispose(bool disposing) { if (!disposedValue) { if (disposing) { // TODO: dispose managed state (managed objects) _outputService.MessageReceived -= OnMessageReceived; Outputs.Clear(); } // TODO: free unmanaged resources (unmanaged objects) and override finalizer // TODO: set large fields to null disposedValue = true; } } // // TODO: override finalizer only if 'Dispose(bool disposing)' has code to free unmanaged resources // ~OutputBarViewModel() // { // // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method // Dispose(disposing: false); // } public void Dispose() { // Do not change this code. Put cleanup code in 'Dispose(bool disposing)' method Dispose(disposing: true); GC.SuppressFinalize(this); } }