MainPage.xaml.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. using LogoForceTestApp.Modules.MainModule.Models;
  2. using Prism.Events;
  3. using System;
  4. using System.Windows.Controls;
  5. using System.Windows.Threading;
  6. namespace LogoForceTestApp.Modules.MainModule.Views
  7. {
  8. /// <summary>
  9. /// MainPage.xaml 的交互逻辑
  10. /// </summary>
  11. public partial class MainPage : UserControl
  12. {
  13. private readonly IEventAggregator _eventAggregator;
  14. public MainPage(IEventAggregator eventAggregator)
  15. {
  16. _eventAggregator = eventAggregator;
  17. _eventAggregator.GetEvent<LogEvent>().Subscribe(WriteLog);
  18. InitializeComponent();
  19. }
  20. private void WriteLog(Tuple<LogType, string> log)
  21. {
  22. ListBoxMessage?.Dispatcher?.BeginInvoke(DispatcherPriority.Render, new Action(() =>
  23. {
  24. if (ListBoxMessage.Items.Count > 100)//24
  25. {
  26. ListBoxMessage.Items.RemoveAt(0);
  27. }
  28. var (type, info) = log;
  29. var listBoxItem = new ListBoxItem
  30. {
  31. Content = info
  32. };
  33. if (log.Item1 == LogType.Error)
  34. listBoxItem.Foreground = System.Windows.Media.Brushes.Red;
  35. else if (log.Item1 == LogType.Warning)
  36. listBoxItem.Foreground = System.Windows.Media.Brushes.Yellow;
  37. else
  38. listBoxItem.Foreground = System.Windows.Media.Brushes.DarkOliveGreen;
  39. ListBoxMessage.Items.Add(listBoxItem);
  40. ListBoxMessage.ScrollIntoView(listBoxItem);
  41. }));
  42. }
  43. }
  44. }