LogBarViewModel.cs 785 B

12345678910111213141516171819202122232425262728293031323334
  1. using AlarmInfoServerSim.Services;
  2. using CommunityToolkit.Mvvm.ComponentModel;
  3. using System.Collections.ObjectModel;
  4. namespace AlarmInfoServerSim.ViewModels;
  5. public partial class LogBarViewModel : ObservableObject
  6. {
  7. private readonly ILogService _logService;
  8. [ObservableProperty]
  9. private ObservableCollection<string> _logs;
  10. public LogBarViewModel(ILogService logService)
  11. {
  12. _logService= logService;
  13. _logService.LogReceived += OnLogReceived;
  14. _logs = [];
  15. }
  16. private void OnLogReceived(object? sender, string e)
  17. {
  18. App.Current.Dispatcher.Invoke(() =>
  19. {
  20. if(Logs.Count>=50)
  21. {
  22. Logs.RemoveAt(Logs.Count - 1);
  23. }
  24. Logs.Insert(0, e);
  25. });
  26. }
  27. }