2
0

UdpLogListBox.xaml.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using System;
  2. using System.Collections.ObjectModel;
  3. using System.Windows.Controls;
  4. namespace FastGithub.UI
  5. {
  6. /// <summary>
  7. /// UdpLogListBox.xaml 的交互逻辑
  8. /// </summary>
  9. public partial class UdpLogListBox : UserControl
  10. {
  11. private readonly int maxLogCount = 100;
  12. public ObservableCollection<UdpLog> LogList { get; } = new ObservableCollection<UdpLog>();
  13. public UdpLogListBox()
  14. {
  15. InitializeComponent();
  16. this.DataContext = this;
  17. this.InitUdpLoggerAsync();
  18. }
  19. private async void InitUdpLoggerAsync()
  20. {
  21. while (this.Dispatcher.HasShutdownStarted == false)
  22. {
  23. try
  24. {
  25. var log = await UdpLogger.GetUdpLogAsync();
  26. if (log != null)
  27. {
  28. this.LogList.Insert(0, log);
  29. if (this.LogList.Count > this.maxLogCount)
  30. {
  31. this.LogList.RemoveAt(this.maxLogCount);
  32. }
  33. }
  34. }
  35. catch (Exception)
  36. {
  37. }
  38. }
  39. }
  40. private void MenuItem_Copy_Click(object sender, System.Windows.RoutedEventArgs e)
  41. {
  42. if (this.listBox.SelectedValue is UdpLog udpLog)
  43. {
  44. udpLog.SetToClipboard();
  45. }
  46. }
  47. private void MenuItem_Clear_Click(object sender, System.Windows.RoutedEventArgs e)
  48. {
  49. this.LogList.Clear();
  50. }
  51. }
  52. }