Phase 0: 项目初始化与基础架构

- 创建 .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 文件
This commit is contained in:
2026-05-09 20:26:51 +08:00
parent b33f5e8f29
commit 875e331116
10 changed files with 282 additions and 0 deletions

34
.gitignore vendored Normal file
View File

@@ -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

8
PersonalToolBox/App.xaml Normal file
View File

@@ -0,0 +1,8 @@
<Application x:Class="PersonalToolBox.App"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:PersonalToolBox">
<Application.Resources>
</Application.Resources>
</Application>

View File

@@ -0,0 +1,38 @@
using Microsoft.Extensions.DependencyInjection;
using System.Windows;
using PersonalToolBox.Services;
using PersonalToolBox.Views;
namespace PersonalToolBox;
/// <summary>
/// WPF 应用程序入口,负责依赖注入容器初始化和启动主窗口
/// </summary>
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>();
mainWindow.Show();
}
/// <summary>
/// 注册所有服务到 DI 容器(单例模式)
/// </summary>
private static void ConfigureServices(IServiceCollection services)
{
// 日志服务
services.AddSingleton<ILogService, LogService>();
// 主窗口
services.AddSingleton<MainWindow>();
}
}

View File

@@ -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)
)]

View File

@@ -0,0 +1,50 @@
namespace PersonalToolBox.Models;
/// <summary>
/// 日志级别
/// </summary>
public enum LogLevel
{
Info,
Warning,
Error
}
/// <summary>
/// 日志条目模型,用于底部信息栏的数据绑定
/// </summary>
public class LogEntry
{
/// <summary>
/// 日志产生时间
/// </summary>
public DateTime Timestamp { get; set; } = DateTime.Now;
/// <summary>
/// 日志级别 (Info / Warning / Error)
/// </summary>
public LogLevel Level { get; set; }
/// <summary>
/// 日志文本内容
/// </summary>
public string Content { get; set; } = string.Empty;
/// <summary>
/// 格式化后的时间字符串 (yyyy-MM-dd HH:mm:ss)
/// </summary>
public string FormattedTime => Timestamp.ToString("yyyy-MM-dd HH:mm:ss");
/// <summary>
/// 日志级别的中文标记
/// </summary>
public string LevelText => Level switch
{
LogLevel.Info => "[信息]",
LogLevel.Warning => "[警告]",
LogLevel.Error => "[错误]",
_ => "[未知]"
};
public override string ToString() => $"[{FormattedTime}] {LevelText} {Content}";
}

View File

@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>WinExe</OutputType>
<TargetFramework>net8.0-windows</TargetFramework>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
<UseWPF>true</UseWPF>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.4.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="10.0.7" />
</ItemGroup>
</Project>

View File

@@ -0,0 +1,35 @@
using System.Collections.ObjectModel;
using PersonalToolBox.Models;
namespace PersonalToolBox.Services;
/// <summary>
/// 日志服务接口,管理底部日志打印栏的数据
/// </summary>
public interface ILogService
{
/// <summary>
/// 日志条目集合,绑定到 UI 的 ObservableCollection
/// </summary>
ObservableCollection<LogEntry> Logs { get; }
/// <summary>
/// 记录普通信息日志
/// </summary>
void Info(string message);
/// <summary>
/// 记录警告日志
/// </summary>
void Warning(string message);
/// <summary>
/// 记录错误日志
/// </summary>
void Error(string message);
/// <summary>
/// 清空所有日志
/// </summary>
void Clear();
}

View File

@@ -0,0 +1,66 @@
using System.Collections.ObjectModel;
using System.Windows;
using PersonalToolBox.Models;
namespace PersonalToolBox.Services;
/// <summary>
/// 日志服务实现,管理底部日志打印栏数据
/// 通过 ObservableCollection 将日志实时推送到 UI
/// </summary>
public class LogService : ILogService
{
public ObservableCollection<LogEntry> 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();
}
/// <summary>
/// 将日志条目安全地添加到 UI 线程的集合中
/// </summary>
private void AddLog(LogEntry entry)
{
if (Application.Current != null)
Application.Current.Dispatcher.Invoke(() => Logs.Add(entry));
else
Logs.Add(entry);
}
}

View File

@@ -0,0 +1,13 @@
<Window x:Class="PersonalToolBox.Views.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:PersonalToolBox.Views"
mc:Ignorable="d"
Title="个人工具箱" Height="600" Width="900"
WindowStartupLocation="CenterScreen">
<Grid>
</Grid>
</Window>

View File

@@ -0,0 +1,11 @@
using System.Windows;
namespace PersonalToolBox.Views;
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
}