From 875e3311169ac67e4f0b93af07b32e12717e37ba Mon Sep 17 00:00:00 2001 From: home-PC Date: Sat, 9 May 2026 20:26:51 +0800 Subject: [PATCH] =?UTF-8?q?Phase=200:=20=E9=A1=B9=E7=9B=AE=E5=88=9D?= =?UTF-8?q?=E5=A7=8B=E5=8C=96=E4=B8=8E=E5=9F=BA=E7=A1=80=E6=9E=B6=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 创建 .NET 8 WPF 项目 PersonalToolBox - 引入 NuGet 包: CommunityToolkit.Mvvm, System.Text.Json, Microsoft.Extensions.DependencyInjection - 建立 MVVM 目录结构: Models/ ViewModels/ Views/ Services/ Helpers/ - 实现 LogEntry 模型 (时间/日志级别/内容) - 实现 ILogService 接口与 LogService (线程安全, 通过 Dispatcher 推送到 UI) - 配置 DI 容器 (App.xaml.cs) 注册日志服务和主窗口 - 添加 .gitignore 文件 --- .gitignore | 34 ++++++++++++ PersonalToolBox/App.xaml | 8 +++ PersonalToolBox/App.xaml.cs | 38 ++++++++++++++ PersonalToolBox/AssemblyInfo.cs | 10 ++++ PersonalToolBox/Models/LogEntry.cs | 50 ++++++++++++++++++ PersonalToolBox/PersonalToolBox.csproj | 17 ++++++ PersonalToolBox/Services/ILogService.cs | 35 +++++++++++++ PersonalToolBox/Services/LogService.cs | 66 ++++++++++++++++++++++++ PersonalToolBox/Views/MainWindow.xaml | 13 +++++ PersonalToolBox/Views/MainWindow.xaml.cs | 11 ++++ 10 files changed, 282 insertions(+) create mode 100644 .gitignore create mode 100644 PersonalToolBox/App.xaml create mode 100644 PersonalToolBox/App.xaml.cs create mode 100644 PersonalToolBox/AssemblyInfo.cs create mode 100644 PersonalToolBox/Models/LogEntry.cs create mode 100644 PersonalToolBox/PersonalToolBox.csproj create mode 100644 PersonalToolBox/Services/ILogService.cs create mode 100644 PersonalToolBox/Services/LogService.cs create mode 100644 PersonalToolBox/Views/MainWindow.xaml create mode 100644 PersonalToolBox/Views/MainWindow.xaml.cs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cf50479 --- /dev/null +++ b/.gitignore @@ -0,0 +1,34 @@ +# .NET / Visual Studio +bin/ +obj/ +*.user +*.suo +*.vs/ +*.DotSettings.user +*.sln.docstates + +# NuGet +*.nupkg +**/packages/* + +# Build results +*.exe +*.dll +*.pdb +*.cache +*.publish/ + +# Rider +.idea/ +*.sln.iml + +# VS Code +.vscode/ + +# OS +.DS_Store +Thumbs.db + +# Environment +*.env +*.env.local diff --git a/PersonalToolBox/App.xaml b/PersonalToolBox/App.xaml new file mode 100644 index 0000000..1fd3954 --- /dev/null +++ b/PersonalToolBox/App.xaml @@ -0,0 +1,8 @@ + + + + + diff --git a/PersonalToolBox/App.xaml.cs b/PersonalToolBox/App.xaml.cs new file mode 100644 index 0000000..c958f89 --- /dev/null +++ b/PersonalToolBox/App.xaml.cs @@ -0,0 +1,38 @@ +using Microsoft.Extensions.DependencyInjection; +using System.Windows; +using PersonalToolBox.Services; +using PersonalToolBox.Views; + +namespace PersonalToolBox; + +/// +/// WPF 应用程序入口,负责依赖注入容器初始化和启动主窗口 +/// +public partial class App : Application +{ + public static IServiceProvider Services { get; private set; } = null!; + + protected override void OnStartup(StartupEventArgs e) + { + base.OnStartup(e); + + var services = new ServiceCollection(); + ConfigureServices(services); + Services = services.BuildServiceProvider(); + + var mainWindow = Services.GetRequiredService(); + mainWindow.Show(); + } + + /// + /// 注册所有服务到 DI 容器(单例模式) + /// + private static void ConfigureServices(IServiceCollection services) + { + // 日志服务 + services.AddSingleton(); + + // 主窗口 + services.AddSingleton(); + } +} diff --git a/PersonalToolBox/AssemblyInfo.cs b/PersonalToolBox/AssemblyInfo.cs new file mode 100644 index 0000000..cc29e7f --- /dev/null +++ b/PersonalToolBox/AssemblyInfo.cs @@ -0,0 +1,10 @@ +using System.Windows; + +[assembly:ThemeInfo( + ResourceDictionaryLocation.None, //where theme specific resource dictionaries are located + //(used if a resource is not found in the page, + // or application resource dictionaries) + ResourceDictionaryLocation.SourceAssembly //where the generic resource dictionary is located + //(used if a resource is not found in the page, + // app, or any theme specific resource dictionaries) +)] diff --git a/PersonalToolBox/Models/LogEntry.cs b/PersonalToolBox/Models/LogEntry.cs new file mode 100644 index 0000000..808d760 --- /dev/null +++ b/PersonalToolBox/Models/LogEntry.cs @@ -0,0 +1,50 @@ +namespace PersonalToolBox.Models; + +/// +/// 日志级别 +/// +public enum LogLevel +{ + Info, + Warning, + Error +} + +/// +/// 日志条目模型,用于底部信息栏的数据绑定 +/// +public class LogEntry +{ + /// + /// 日志产生时间 + /// + public DateTime Timestamp { get; set; } = DateTime.Now; + + /// + /// 日志级别 (Info / Warning / Error) + /// + public LogLevel Level { get; set; } + + /// + /// 日志文本内容 + /// + public string Content { get; set; } = string.Empty; + + /// + /// 格式化后的时间字符串 (yyyy-MM-dd HH:mm:ss) + /// + public string FormattedTime => Timestamp.ToString("yyyy-MM-dd HH:mm:ss"); + + /// + /// 日志级别的中文标记 + /// + public string LevelText => Level switch + { + LogLevel.Info => "[信息]", + LogLevel.Warning => "[警告]", + LogLevel.Error => "[错误]", + _ => "[未知]" + }; + + public override string ToString() => $"[{FormattedTime}] {LevelText} {Content}"; +} diff --git a/PersonalToolBox/PersonalToolBox.csproj b/PersonalToolBox/PersonalToolBox.csproj new file mode 100644 index 0000000..b361992 --- /dev/null +++ b/PersonalToolBox/PersonalToolBox.csproj @@ -0,0 +1,17 @@ + + + + WinExe + net8.0-windows + enable + enable + true + + + + + + + + + diff --git a/PersonalToolBox/Services/ILogService.cs b/PersonalToolBox/Services/ILogService.cs new file mode 100644 index 0000000..2ed431c --- /dev/null +++ b/PersonalToolBox/Services/ILogService.cs @@ -0,0 +1,35 @@ +using System.Collections.ObjectModel; +using PersonalToolBox.Models; + +namespace PersonalToolBox.Services; + +/// +/// 日志服务接口,管理底部日志打印栏的数据 +/// +public interface ILogService +{ + /// + /// 日志条目集合,绑定到 UI 的 ObservableCollection + /// + ObservableCollection Logs { get; } + + /// + /// 记录普通信息日志 + /// + void Info(string message); + + /// + /// 记录警告日志 + /// + void Warning(string message); + + /// + /// 记录错误日志 + /// + void Error(string message); + + /// + /// 清空所有日志 + /// + void Clear(); +} diff --git a/PersonalToolBox/Services/LogService.cs b/PersonalToolBox/Services/LogService.cs new file mode 100644 index 0000000..432e048 --- /dev/null +++ b/PersonalToolBox/Services/LogService.cs @@ -0,0 +1,66 @@ +using System.Collections.ObjectModel; +using System.Windows; +using PersonalToolBox.Models; + +namespace PersonalToolBox.Services; + +/// +/// 日志服务实现,管理底部日志打印栏数据 +/// 通过 ObservableCollection 将日志实时推送到 UI +/// +public class LogService : ILogService +{ + public ObservableCollection Logs { get; } = new(); + + public void Info(string message) + { + var entry = new LogEntry + { + Timestamp = DateTime.Now, + Level = LogLevel.Info, + Content = message + }; + AddLog(entry); + } + + public void Warning(string message) + { + var entry = new LogEntry + { + Timestamp = DateTime.Now, + Level = LogLevel.Warning, + Content = message + }; + AddLog(entry); + } + + public void Error(string message) + { + var entry = new LogEntry + { + Timestamp = DateTime.Now, + Level = LogLevel.Error, + Content = message + }; + AddLog(entry); + } + + public void Clear() + { + if (Application.Current != null) + Application.Current.Dispatcher.Invoke(() => Logs.Clear()); + else + Logs.Clear(); + } + + /// + /// 将日志条目安全地添加到 UI 线程的集合中 + /// + private void AddLog(LogEntry entry) + { + if (Application.Current != null) + Application.Current.Dispatcher.Invoke(() => Logs.Add(entry)); + else + Logs.Add(entry); + } +} diff --git a/PersonalToolBox/Views/MainWindow.xaml b/PersonalToolBox/Views/MainWindow.xaml new file mode 100644 index 0000000..12e377d --- /dev/null +++ b/PersonalToolBox/Views/MainWindow.xaml @@ -0,0 +1,13 @@ + + + + + diff --git a/PersonalToolBox/Views/MainWindow.xaml.cs b/PersonalToolBox/Views/MainWindow.xaml.cs new file mode 100644 index 0000000..3b33c9a --- /dev/null +++ b/PersonalToolBox/Views/MainWindow.xaml.cs @@ -0,0 +1,11 @@ +using System.Windows; + +namespace PersonalToolBox.Views; + +public partial class MainWindow : Window +{ + public MainWindow() + { + InitializeComponent(); + } +}