ViewRealLogDlg.xaml.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using MeterVision.Config;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.ComponentModel;
  6. using System.IO;
  7. using System.Linq;
  8. using System.Text;
  9. using System.Threading.Tasks;
  10. using System.Windows;
  11. using System.Windows.Controls;
  12. using System.Windows.Data;
  13. using System.Windows.Documents;
  14. using System.Windows.Input;
  15. using System.Windows.Media;
  16. using System.Windows.Media.Imaging;
  17. using System.Windows.Shapes;
  18. namespace MeterVision.Dlg
  19. {
  20. /// <summary>
  21. /// ViewRealLogDlg.xaml 的交互逻辑
  22. /// </summary>
  23. public partial class ViewRealLogDlg : Window, INotifyPropertyChanged
  24. {
  25. public event PropertyChangedEventHandler PropertyChanged;
  26. protected void OnPropertyChanged(string propertyName)
  27. {
  28. PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  29. }
  30. public ObservableCollection<FileInfo> FileList { get; set; } = new ObservableCollection<FileInfo>();
  31. public int TotalRecords => FileList.Count;
  32. //public string SelctedFileName { get; set; }
  33. private FileInfo _selectedFileInfo;
  34. public FileInfo SelectedFileInfo
  35. {
  36. get => _selectedFileInfo;
  37. set
  38. {
  39. if(_selectedFileInfo != value)
  40. {
  41. _selectedFileInfo = value;
  42. OnPropertyChanged(nameof(SelectedFileInfo));
  43. ShowRealLog(_selectedFileInfo);
  44. }
  45. }
  46. }
  47. public ViewRealLogDlg()
  48. {
  49. InitializeComponent();
  50. lvFileName.ItemsSource = FileList; //绑定到ListView数据源
  51. LoadFiles();
  52. this.DataContext = this;
  53. }
  54. /// <summary>
  55. /// 调用异步加载方法
  56. /// </summary>
  57. private async void LoadFiles()
  58. {
  59. List<FileInfo> files = await LoadTxtFilesAsync();
  60. FileList.Clear();
  61. foreach (var file in files)
  62. {
  63. FileList.Add(file);
  64. }
  65. OnPropertyChanged(nameof(TotalRecords));
  66. }
  67. public async Task<List<FileInfo>> LoadTxtFilesAsync()
  68. {
  69. string directoryPath = CfginiItem.GetConfigItem().RealLogPath;
  70. return await Task.Run(() =>
  71. {
  72. if (!Directory.Exists(directoryPath))
  73. return new List<FileInfo>();
  74. return new DirectoryInfo(directoryPath)
  75. .GetFiles("*.txt", SearchOption.TopDirectoryOnly) // 获取所有 .txt 文件
  76. .Where(f => f.Length > 0) // 过滤掉大小为 0 的文件
  77. .OrderByDescending(f => f.LastWriteTime) // 按修改时间排序
  78. .Take(100)
  79. .ToList();
  80. });
  81. }
  82. //显示日志
  83. private void ShowRealLog(FileInfo fileInfo)
  84. {
  85. RichTxtContentBox.Document.Blocks.Clear();
  86. RichTxtContentBox.UpdateLayout();
  87. RichTxtContentBox.ScrollToHome();
  88. if (fileInfo == null) return;
  89. string filePath = fileInfo.FullName;
  90. try
  91. {
  92. if (File.Exists(filePath))
  93. {
  94. string content = File.ReadAllText(filePath);
  95. // 创建段落并将日志内容添加到段落
  96. Paragraph logParagraph = new Paragraph(new Run(content));
  97. // 将日志段落添加到 RichTextBox 的文档中
  98. RichTxtContentBox.Document.Blocks.Add(logParagraph);
  99. }
  100. else
  101. {
  102. MessageBox.Show("日志文件未找到!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
  103. }
  104. }
  105. catch (Exception ex)
  106. {
  107. MessageBox.Show($"加载日志文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
  108. }
  109. }
  110. private void LvFileName_SelectionChanged(object sender, SelectionChangedEventArgs e)
  111. {
  112. }
  113. private void BtnClose_Click(object sender, RoutedEventArgs e)
  114. {
  115. this.Close();
  116. }
  117. //--------------------------------------------------------------------------------
  118. }
  119. }