From d0ded5cd9bf5070a120bad58b5be21fe2ac6a4ff Mon Sep 17 00:00:00 2001
From: C3032 <C3032@BC3032>
Date: 星期六, 20 十二月 2025 16:41:09 +0800
Subject: [PATCH] test
---
IDViewer_2D/ViewModel/LogViewModel.cs | 182 +++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 182 insertions(+), 0 deletions(-)
diff --git a/IDViewer_2D/ViewModel/LogViewModel.cs b/IDViewer_2D/ViewModel/LogViewModel.cs
new file mode 100644
index 0000000..02ac409
--- /dev/null
+++ b/IDViewer_2D/ViewModel/LogViewModel.cs
@@ -0,0 +1,182 @@
+using System;
+using System.Collections.Generic;
+using System.Collections.ObjectModel;
+using System.ComponentModel;
+using System.Linq;
+using System.Runtime.CompilerServices;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media;
+using SmartScanner;
+
+namespace SmartScanner.ViewModel
+{
+ public class LogEntry : INotifyPropertyChanged
+ {
+ private DateTime _timestamp;
+ public DateTime Timestamp
+ {
+ get => _timestamp;
+ set
+ {
+ _timestamp = value;
+ OnPropertyChanged();
+ }
+ }
+
+ private string _level;
+ public string Level
+ {
+ get => _level;
+ set
+ {
+ _level = value;
+ OnPropertyChanged();
+ }
+ }
+
+ private string _message;
+ public string Message
+ {
+ get => _message;
+ set
+ {
+ _message = value;
+ OnPropertyChanged();
+ }
+ }
+
+ private Brush _foreground;
+ public Brush Foreground
+ {
+ get => _foreground;
+ set
+ {
+ _foreground = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+
+ public class EnhancedLogViewModel : INotifyPropertyChanged
+ {
+ public ObservableCollection<LogEntry> LogEntries { get; } = new ObservableCollection<LogEntry>();
+ private static readonly Lazy<EnhancedLogViewModel> _instance =
+ new Lazy<EnhancedLogViewModel>(() => new EnhancedLogViewModel());
+
+ public static EnhancedLogViewModel Instance => _instance.Value;
+
+ // 鏃ュ織鑷姩淇濆瓨鏈嶅姟
+ private LogAutoSaveService _logAutoSaveService;
+
+ private EnhancedLogViewModel()
+ {
+ // 鍒濆鍖栨棩蹇楄嚜鍔ㄤ繚瀛樻湇鍔�
+ _logAutoSaveService = new LogAutoSaveService(LogEntries);
+ }
+
+ // 娣诲姞瀵筍crollViewer鐨勫紩鐢�
+ private ScrollViewer _logScrollViewer;
+ public ScrollViewer LogScrollViewer
+ {
+ get => _logScrollViewer;
+ set
+ {
+ _logScrollViewer = value;
+ OnPropertyChanged();
+ }
+ }
+
+ public void AddLog(string message, string level = "INFO")
+ {
+ Application.Current.Dispatcher.Invoke(() =>
+ {
+ var entry = new LogEntry
+ {
+ Timestamp = DateTime.Now,
+ Level = level,
+ Message = message,
+ Foreground = GetLevelColor(level)
+ };
+
+ LogEntries.Add(entry);
+
+ // 闄愬埗鏃ュ織鏁伴噺
+ if (LogEntries.Count > 500)
+ {
+ LogEntries.RemoveAt(0);
+ }
+
+ // 閫氱煡鑷姩淇濆瓨鏈嶅姟鏈夋柊鏃ュ織锛屼紶閫扡ogEntry瀵硅薄
+ _logAutoSaveService?.LogAdded(entry);
+
+ // 婊氬姩鍒板簳閮�
+ ScrollToBottom();
+ });
+ }
+
+ /// <summary>
+ /// 鎵嬪姩淇濆瓨褰撳墠鏃ュ織
+ /// </summary>
+ public void SaveLogs()
+ {
+ _logAutoSaveService?.ForceSave();
+ }
+
+ /// <summary>
+ /// 鍋滄鏃ュ織鑷姩淇濆瓨鏈嶅姟
+ /// </summary>
+ public void StopAutoSaveService()
+ {
+ _logAutoSaveService?.Stop();
+ _logAutoSaveService = null;
+ }
+
+ /// <summary>
+ /// 鑾峰彇鑷姩淇濆瓨鏈嶅姟鐘舵��
+ /// </summary>
+ public string GetAutoSaveStatus()
+ {
+ return _logAutoSaveService?.GetStatus() ?? "鑷姩淇濆瓨鏈嶅姟鏈繍琛�";
+ }
+
+ private void ScrollToBottom()
+ {
+ if (LogScrollViewer != null)
+ {
+ LogScrollViewer.ScrollToEnd();
+ }
+ }
+
+ private Brush GetLevelColor(string level)
+ {
+ switch (level)
+ {
+ case "ERROR":
+ return Brushes.Red;
+ case "WARN":
+ return Brushes.Orange;
+ case "DEBUG":
+ return Brushes.Blue;
+ default:
+ return Brushes.Black;
+ }
+ }
+
+ public event PropertyChangedEventHandler PropertyChanged;
+
+ protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
+ {
+ PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
+ }
+ }
+}
--
Gitblit v1.9.3