123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139 |
- using MeterVision.Config;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.ComponentModel;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Shapes;
- namespace MeterVision.Dlg
- {
- /// <summary>
- /// ViewRealLogDlg.xaml 的交互逻辑
- /// </summary>
- public partial class ViewRealLogDlg : Window, INotifyPropertyChanged
- {
- public event PropertyChangedEventHandler PropertyChanged;
- protected void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }
- public ObservableCollection<FileInfo> FileList { get; set; } = new ObservableCollection<FileInfo>();
- public int TotalRecords => FileList.Count;
- //public string SelctedFileName { get; set; }
- private FileInfo _selectedFileInfo;
- public FileInfo SelectedFileInfo
- {
- get => _selectedFileInfo;
- set
- {
- if(_selectedFileInfo != value)
- {
- _selectedFileInfo = value;
- OnPropertyChanged(nameof(SelectedFileInfo));
- ShowRealLog(_selectedFileInfo);
- }
- }
- }
- public ViewRealLogDlg()
- {
- InitializeComponent();
- lvFileName.ItemsSource = FileList; //绑定到ListView数据源
- LoadFiles();
-
- this.DataContext = this;
- }
- /// <summary>
- /// 调用异步加载方法
- /// </summary>
- private async void LoadFiles()
- {
- List<FileInfo> files = await LoadTxtFilesAsync();
- FileList.Clear();
- foreach (var file in files)
- {
- FileList.Add(file);
- }
- OnPropertyChanged(nameof(TotalRecords));
- }
- public async Task<List<FileInfo>> LoadTxtFilesAsync()
- {
- string directoryPath = CfginiItem.GetConfigItem().RealLogPath;
- return await Task.Run(() =>
- {
- if (!Directory.Exists(directoryPath))
- return new List<FileInfo>();
- return new DirectoryInfo(directoryPath)
- .GetFiles("*.txt", SearchOption.TopDirectoryOnly) // 获取所有 .txt 文件
- .Where(f => f.Length > 0) // 过滤掉大小为 0 的文件
- .OrderByDescending(f => f.LastWriteTime) // 按修改时间排序
- .Take(100)
- .ToList();
- });
- }
- //显示日志
- private void ShowRealLog(FileInfo fileInfo)
- {
- RichTxtContentBox.Document.Blocks.Clear();
- RichTxtContentBox.UpdateLayout();
- RichTxtContentBox.ScrollToHome();
- if (fileInfo == null) return;
- string filePath = fileInfo.FullName;
- try
- {
- if (File.Exists(filePath))
- {
- string content = File.ReadAllText(filePath);
- // 创建段落并将日志内容添加到段落
- Paragraph logParagraph = new Paragraph(new Run(content));
- // 将日志段落添加到 RichTextBox 的文档中
- RichTxtContentBox.Document.Blocks.Add(logParagraph);
- }
- else
- {
- MessageBox.Show("日志文件未找到!", "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show($"加载日志文件时出错: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
- }
- }
- private void LvFileName_SelectionChanged(object sender, SelectionChangedEventArgs e)
- {
- }
- private void BtnClose_Click(object sender, RoutedEventArgs e)
- {
- this.Close();
- }
- //--------------------------------------------------------------------------------
- }
- }
|