Phase 2-3: UI layout, theme switching, CRUD tools, process execution
- Phase 2: MainWindow 3-section layout (sidebar/content/log bar), Dark/Light theme with ThemeHelper, MainViewModel with ObservableProperty/RelayCommand, tool card filtering by search + category - Phase 3: ToolEditWindow for add/edit tools, ProcessExecutionService (Process.Start + error handling), double-click + right-click context menu (run/edit), path browse dialog - Bugfix: ContextMenu commands now use PlacementTarget.Tag binding (ContextMenu in separate visual tree) - Bugfix: StaticResource converters moved to XAML before DataTemplate to fix XamlParseException on tool card render - Bugfix: Pure filenames (no path separators) treated as PATH commands, not marked invalid - Bugfix: RefreshData preserves SelectedCategory; Load() catches all exceptions; Save() wrapped in try-catch; auto-scroll log to newest entry - Tests: xUnit project with 55 tests covering models, services, converters, and view models
This commit is contained in:
@@ -3,6 +3,10 @@
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:local="clr-namespace:PersonalToolBox">
|
||||
<Application.Resources>
|
||||
|
||||
<ResourceDictionary>
|
||||
<ResourceDictionary.MergedDictionaries>
|
||||
<ResourceDictionary Source="Themes/DarkTheme.xaml"/>
|
||||
</ResourceDictionary.MergedDictionaries>
|
||||
</ResourceDictionary>
|
||||
</Application.Resources>
|
||||
</Application>
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.IO;
|
||||
using System.Windows;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PersonalToolBox.Services;
|
||||
using PersonalToolBox.Views;
|
||||
|
||||
@@ -12,34 +13,81 @@ public partial class App : Application
|
||||
{
|
||||
public static IServiceProvider Services { get; private set; } = null!;
|
||||
|
||||
private static readonly string CrashLogPath = Path.Combine(
|
||||
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
|
||||
"PersonalToolBox", "crash.log");
|
||||
|
||||
public App()
|
||||
{
|
||||
// 监听未处理的异常,写入文件日志
|
||||
DispatcherUnhandledException += (s, e) =>
|
||||
{
|
||||
WriteCrashLog($"UI线程异常: {e.Exception}");
|
||||
e.Handled = true;
|
||||
MessageBox.Show($"发生未处理异常:\n{e.Exception.Message}\n\n详情已写入:\n{CrashLogPath}",
|
||||
"错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
};
|
||||
|
||||
AppDomain.CurrentDomain.UnhandledException += (s, e) =>
|
||||
{
|
||||
WriteCrashLog($"未处理异常: {e.ExceptionObject}");
|
||||
};
|
||||
}
|
||||
|
||||
protected override void OnStartup(StartupEventArgs e)
|
||||
{
|
||||
base.OnStartup(e);
|
||||
try
|
||||
{
|
||||
base.OnStartup(e);
|
||||
|
||||
var services = new ServiceCollection();
|
||||
ConfigureServices(services);
|
||||
Services = services.BuildServiceProvider();
|
||||
var services = new ServiceCollection();
|
||||
ConfigureServices(services);
|
||||
Services = services.BuildServiceProvider();
|
||||
|
||||
// 启动时加载配置文件(含路径验证与容错)
|
||||
var dataService = Services.GetRequiredService<IDataService>();
|
||||
dataService.Load();
|
||||
// 启动时加载配置文件(含路径验证与容错)
|
||||
var dataService = Services.GetRequiredService<IDataService>();
|
||||
dataService.Load();
|
||||
|
||||
var mainWindow = Services.GetRequiredService<MainWindow>();
|
||||
mainWindow.Show();
|
||||
var mainWindow = Services.GetRequiredService<MainWindow>();
|
||||
mainWindow.Show();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
WriteCrashLog($"启动失败: {ex}");
|
||||
MessageBox.Show($"启动失败:\n{ex.Message}\n\n详情已写入:\n{CrashLogPath}",
|
||||
"启动错误", MessageBoxButton.OK, MessageBoxImage.Error);
|
||||
Shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 注册所有服务到 DI 容器(单例模式)
|
||||
/// 注册所有服务到 DI 容器
|
||||
/// </summary>
|
||||
private static void ConfigureServices(IServiceCollection services)
|
||||
{
|
||||
// 日志服务
|
||||
services.AddSingleton<ILogService, LogService>();
|
||||
|
||||
// 数据持久化服务
|
||||
services.AddSingleton<IDataService, JsonDataService>();
|
||||
|
||||
// 主窗口
|
||||
services.AddSingleton<IProcessExecutionService, ProcessExecutionService>();
|
||||
services.AddSingleton<ViewModels.MainViewModel>();
|
||||
services.AddTransient<ViewModels.ToolEditViewModel>();
|
||||
services.AddSingleton<MainWindow>();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 将崩溃信息写入文件日志
|
||||
/// </summary>
|
||||
public static void WriteCrashLog(string message)
|
||||
{
|
||||
try
|
||||
{
|
||||
var dir = Path.GetDirectoryName(CrashLogPath);
|
||||
if (dir != null) Directory.CreateDirectory(dir);
|
||||
File.AppendAllText(CrashLogPath,
|
||||
$"[{DateTime.Now:yyyy-MM-dd HH:mm:ss}] {message}{Environment.NewLine}{Environment.NewLine}");
|
||||
}
|
||||
catch
|
||||
{
|
||||
// 无法写入日志时静默忽略
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
55
PersonalToolBox/Helpers/ThemeHelper.cs
Normal file
55
PersonalToolBox/Helpers/ThemeHelper.cs
Normal file
@@ -0,0 +1,55 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows;
|
||||
|
||||
namespace PersonalToolBox.Helpers;
|
||||
|
||||
/// <summary>
|
||||
/// 主题切换辅助类,动态替换 Application 的 ResourceDictionary
|
||||
/// </summary>
|
||||
public static class ThemeHelper
|
||||
{
|
||||
private const string ThemePrefix = "Themes/";
|
||||
private const string DarkThemePath = "Themes/DarkTheme.xaml";
|
||||
private const string LightThemePath = "Themes/LightTheme.xaml";
|
||||
|
||||
/// <summary>
|
||||
/// 切换主题(Dark / Light)
|
||||
/// </summary>
|
||||
public static void ApplyTheme(string theme)
|
||||
{
|
||||
var app = Application.Current;
|
||||
if (app == null) return;
|
||||
|
||||
// 移除旧的主题资源字典
|
||||
var oldDict = FindThemeDictionary(app.Resources.MergedDictionaries);
|
||||
if (oldDict != null)
|
||||
app.Resources.MergedDictionaries.Remove(oldDict);
|
||||
|
||||
// 加载新的主题资源字典
|
||||
var path = theme switch
|
||||
{
|
||||
"Light" => LightThemePath,
|
||||
_ => DarkThemePath
|
||||
};
|
||||
|
||||
var newDict = new ResourceDictionary
|
||||
{
|
||||
Source = new Uri(path, UriKind.Relative)
|
||||
};
|
||||
app.Resources.MergedDictionaries.Add(newDict);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 在当前合并字典中查找主题相关的 ResourceDictionary
|
||||
/// </summary>
|
||||
private static ResourceDictionary? FindThemeDictionary(Collection<ResourceDictionary> dictionaries)
|
||||
{
|
||||
foreach (var dict in dictionaries)
|
||||
{
|
||||
if (dict.Source != null && dict.Source.OriginalString.StartsWith(ThemePrefix))
|
||||
return dict;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
15
PersonalToolBox/Services/IProcessExecutionService.cs
Normal file
15
PersonalToolBox/Services/IProcessExecutionService.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using PersonalToolBox.Models;
|
||||
|
||||
namespace PersonalToolBox.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 进程执行服务接口,负责启动外部工具进程
|
||||
/// </summary>
|
||||
public interface IProcessExecutionService
|
||||
{
|
||||
/// <summary>
|
||||
/// 执行指定的工具项
|
||||
/// </summary>
|
||||
/// <param name="tool">要执行的工具项</param>
|
||||
void Execute(ToolItem tool);
|
||||
}
|
||||
@@ -65,11 +65,22 @@ public class JsonDataService : IDataService
|
||||
continue;
|
||||
}
|
||||
|
||||
// 纯文件名(无路径分隔符)可能位于系统 PATH 中,不标记为失效
|
||||
if (!tool.ExecutablePath.Contains('\\') && !tool.ExecutablePath.Contains('/'))
|
||||
{
|
||||
tool.IsValid = true;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!File.Exists(tool.ExecutablePath))
|
||||
{
|
||||
tool.IsValid = false;
|
||||
_logService.Warning($"工具 \"{tool.Name}\" 路径失效,找不到文件: {tool.ExecutablePath}");
|
||||
}
|
||||
else
|
||||
{
|
||||
tool.IsValid = true;
|
||||
}
|
||||
}
|
||||
|
||||
_logService.Info($"配置加载完成: {Config.Categories.Count} 个分类, {Config.Tools.Count} 个工具");
|
||||
@@ -79,6 +90,11 @@ public class JsonDataService : IDataService
|
||||
_logService.Error($"配置文件 JSON 解析失败: {ex.Message}");
|
||||
Config = new AppConfig();
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logService.Error($"配置文件加载失败: {ex.Message}");
|
||||
Config = new AppConfig();
|
||||
}
|
||||
}
|
||||
|
||||
public void Save()
|
||||
|
||||
49
PersonalToolBox/Services/ProcessExecutionService.cs
Normal file
49
PersonalToolBox/Services/ProcessExecutionService.cs
Normal file
@@ -0,0 +1,49 @@
|
||||
using System.Diagnostics;
|
||||
using PersonalToolBox.Models;
|
||||
|
||||
namespace PersonalToolBox.Services;
|
||||
|
||||
/// <summary>
|
||||
/// 进程执行服务,负责启动外部工具进程并处理异常
|
||||
/// </summary>
|
||||
public class ProcessExecutionService : IProcessExecutionService
|
||||
{
|
||||
private readonly ILogService _logService;
|
||||
|
||||
public ProcessExecutionService(ILogService logService)
|
||||
{
|
||||
_logService = logService;
|
||||
}
|
||||
|
||||
public void Execute(ToolItem tool)
|
||||
{
|
||||
if (tool == null)
|
||||
{
|
||||
_logService.Error("尝试执行空工具项");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!tool.IsValid)
|
||||
{
|
||||
_logService.Warning($"无法运行工具 \"{tool.Name}\",路径失效: {tool.ExecutablePath}");
|
||||
return;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
var startInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = tool.ExecutablePath,
|
||||
Arguments = tool.Arguments ?? string.Empty,
|
||||
UseShellExecute = true
|
||||
};
|
||||
|
||||
Process.Start(startInfo);
|
||||
_logService.Info($"成功启动: {tool.Name}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logService.Error($"启动工具 \"{tool.Name}\" 失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
20
PersonalToolBox/Themes/DarkTheme.xaml
Normal file
20
PersonalToolBox/Themes/DarkTheme.xaml
Normal file
@@ -0,0 +1,20 @@
|
||||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
|
||||
<!-- 暗黑主题颜色定义 -->
|
||||
<SolidColorBrush x:Key="Theme.Background" Color="#1E1E2E"/>
|
||||
<SolidColorBrush x:Key="Theme.Foreground" Color="#CDD6F4"/>
|
||||
<SolidColorBrush x:Key="Theme.SidebarBackground" Color="#181825"/>
|
||||
<SolidColorBrush x:Key="Theme.CardBackground" Color="#313244"/>
|
||||
<SolidColorBrush x:Key="Theme.CardBorder" Color="#45475A"/>
|
||||
<SolidColorBrush x:Key="Theme.LogBackground" Color="#11111B"/>
|
||||
<SolidColorBrush x:Key="Theme.Accent" Color="#89B4FA"/>
|
||||
<SolidColorBrush x:Key="Theme.AccentHover" Color="#B4BEFE"/>
|
||||
<SolidColorBrush x:Key="Theme.TextSecondary" Color="#A6ADC8"/>
|
||||
<SolidColorBrush x:Key="Theme.InputBackground" Color="#313244"/>
|
||||
<SolidColorBrush x:Key="Theme.InputBorder" Color="#585B70"/>
|
||||
<SolidColorBrush x:Key="Theme.ButtonBackground" Color="#89B4FA"/>
|
||||
<SolidColorBrush x:Key="Theme.ButtonForeground" Color="#1E1E2E"/>
|
||||
<SolidColorBrush x:Key="Theme.ScrollBarBackground" Color="#313244"/>
|
||||
|
||||
</ResourceDictionary>
|
||||
20
PersonalToolBox/Themes/LightTheme.xaml
Normal file
20
PersonalToolBox/Themes/LightTheme.xaml
Normal file
@@ -0,0 +1,20 @@
|
||||
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
|
||||
|
||||
<!-- 明亮主题颜色定义 -->
|
||||
<SolidColorBrush x:Key="Theme.Background" Color="#EFF1F5"/>
|
||||
<SolidColorBrush x:Key="Theme.Foreground" Color="#4C4F69"/>
|
||||
<SolidColorBrush x:Key="Theme.SidebarBackground" Color="#E6E9EF"/>
|
||||
<SolidColorBrush x:Key="Theme.CardBackground" Color="#FFFFFF"/>
|
||||
<SolidColorBrush x:Key="Theme.CardBorder" Color="#CCD0DA"/>
|
||||
<SolidColorBrush x:Key="Theme.LogBackground" Color="#DCE0E8"/>
|
||||
<SolidColorBrush x:Key="Theme.Accent" Color="#1E66F5"/>
|
||||
<SolidColorBrush x:Key="Theme.AccentHover" Color="#2A6EF5"/>
|
||||
<SolidColorBrush x:Key="Theme.TextSecondary" Color="#6C6F85"/>
|
||||
<SolidColorBrush x:Key="Theme.InputBackground" Color="#FFFFFF"/>
|
||||
<SolidColorBrush x:Key="Theme.InputBorder" Color="#9CA0B0"/>
|
||||
<SolidColorBrush x:Key="Theme.ButtonBackground" Color="#1E66F5"/>
|
||||
<SolidColorBrush x:Key="Theme.ButtonForeground" Color="#FFFFFF"/>
|
||||
<SolidColorBrush x:Key="Theme.ScrollBarBackground" Color="#CCD0DA"/>
|
||||
|
||||
</ResourceDictionary>
|
||||
178
PersonalToolBox/ViewModels/MainViewModel.cs
Normal file
178
PersonalToolBox/ViewModels/MainViewModel.cs
Normal file
@@ -0,0 +1,178 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Linq;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using PersonalToolBox.Helpers;
|
||||
using PersonalToolBox.Models;
|
||||
using PersonalToolBox.Services;
|
||||
using PersonalToolBox.Views;
|
||||
|
||||
namespace PersonalToolBox.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// 主窗口 ViewModel,管理 UI 状态、数据绑定和用户交互逻辑
|
||||
/// </summary>
|
||||
public partial class MainViewModel : ObservableObject
|
||||
{
|
||||
private readonly ILogService _logService;
|
||||
private readonly IDataService _dataService;
|
||||
private readonly IProcessExecutionService _processService;
|
||||
private readonly IServiceProvider _serviceProvider;
|
||||
|
||||
/// <summary>
|
||||
/// 全部分类的虚拟对象
|
||||
/// </summary>
|
||||
private static readonly Category AllCategory = new() { Id = "", Name = "全部" };
|
||||
|
||||
public MainViewModel(
|
||||
ILogService logService,
|
||||
IDataService dataService,
|
||||
IProcessExecutionService processService,
|
||||
IServiceProvider serviceProvider)
|
||||
{
|
||||
_logService = logService;
|
||||
_dataService = dataService;
|
||||
_processService = processService;
|
||||
_serviceProvider = serviceProvider;
|
||||
|
||||
LoadData();
|
||||
}
|
||||
|
||||
// ───────────────────────────── 可观察属性 ─────────────────────────────
|
||||
|
||||
[ObservableProperty]
|
||||
private string _searchText = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private Category? _selectedCategory;
|
||||
|
||||
public ObservableCollection<LogEntry> Logs => _logService.Logs;
|
||||
|
||||
public ObservableCollection<Category> Categories { get; } = new();
|
||||
|
||||
public ObservableCollection<ToolItem> Tools { get; } = new();
|
||||
|
||||
[ObservableProperty]
|
||||
private ObservableCollection<ToolItem> _filteredTools = new();
|
||||
|
||||
[ObservableProperty]
|
||||
private string _currentTheme = "Dark";
|
||||
|
||||
// ───────────────────────────── 命令 ─────────────────────────────
|
||||
|
||||
[RelayCommand]
|
||||
private void ClearLogs() => _logService.Clear();
|
||||
|
||||
[RelayCommand]
|
||||
private void ToggleTheme()
|
||||
{
|
||||
CurrentTheme = CurrentTheme == "Dark" ? "Light" : "Dark";
|
||||
ThemeHelper.ApplyTheme(CurrentTheme);
|
||||
_dataService.Config.Theme = CurrentTheme;
|
||||
_dataService.Save();
|
||||
_logService.Info($"已切换到{(CurrentTheme == "Dark" ? "暗黑" : "明亮")}主题");
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开添加工具弹窗
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void AddTool()
|
||||
{
|
||||
var vm = _serviceProvider.GetRequiredService<ToolEditViewModel>();
|
||||
var window = new ToolEditWindow(vm);
|
||||
window.ShowDialog();
|
||||
|
||||
if (vm.Saved)
|
||||
{
|
||||
RefreshData();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 打开编辑工具弹窗
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void EditTool(ToolItem tool)
|
||||
{
|
||||
if (tool == null) return;
|
||||
|
||||
// 创建编辑 ViewModel(需要新建实例,DI 容器无法区分参数)
|
||||
var dataService = _serviceProvider.GetRequiredService<IDataService>();
|
||||
var logService = _serviceProvider.GetRequiredService<ILogService>();
|
||||
var editVm = new ToolEditViewModel(dataService, logService, tool);
|
||||
var window = new ToolEditWindow(editVm);
|
||||
window.ShowDialog();
|
||||
|
||||
if (editVm.Saved)
|
||||
{
|
||||
RefreshData();
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行工具(双击卡片或右键菜单)
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void ExecuteTool(ToolItem? tool)
|
||||
{
|
||||
if (tool == null) return;
|
||||
_processService.Execute(tool);
|
||||
}
|
||||
|
||||
// ───────────────────────────── 数据刷新 ─────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 从配置重新加载数据并刷新 UI
|
||||
/// </summary>
|
||||
public void RefreshData()
|
||||
{
|
||||
var previousCategoryId = SelectedCategory?.Id;
|
||||
|
||||
Categories.Clear();
|
||||
Categories.Add(AllCategory);
|
||||
|
||||
foreach (var cat in _dataService.Config.Categories)
|
||||
Categories.Add(cat);
|
||||
|
||||
Tools.Clear();
|
||||
foreach (var tool in _dataService.Config.Tools)
|
||||
Tools.Add(tool);
|
||||
|
||||
// 恢复之前选中的分类(若仍存在),否则选中"全部"
|
||||
SelectedCategory = Categories.FirstOrDefault(c => c.Id == previousCategoryId) ?? AllCategory;
|
||||
ApplyFilter();
|
||||
}
|
||||
|
||||
// ───────────────────────────── 初始化 ─────────────────────────────
|
||||
|
||||
private void LoadData()
|
||||
{
|
||||
RefreshData();
|
||||
|
||||
SelectedCategory = AllCategory;
|
||||
CurrentTheme = _dataService.Config.Theme;
|
||||
ThemeHelper.ApplyTheme(CurrentTheme);
|
||||
}
|
||||
|
||||
// ───────────────────────────── 过滤逻辑 ─────────────────────────────
|
||||
|
||||
partial void OnSearchTextChanged(string value) => ApplyFilter();
|
||||
|
||||
partial void OnSelectedCategoryChanged(Category? value) => ApplyFilter();
|
||||
|
||||
private void ApplyFilter()
|
||||
{
|
||||
var filtered = Tools.AsEnumerable();
|
||||
|
||||
if (SelectedCategory != null && !string.IsNullOrEmpty(SelectedCategory.Id))
|
||||
filtered = filtered.Where(t => t.CategoryId == SelectedCategory.Id);
|
||||
|
||||
if (!string.IsNullOrWhiteSpace(SearchText))
|
||||
filtered = filtered.Where(t =>
|
||||
t.Name.Contains(SearchText, System.StringComparison.OrdinalIgnoreCase));
|
||||
|
||||
FilteredTools = new ObservableCollection<ToolItem>(filtered);
|
||||
}
|
||||
}
|
||||
191
PersonalToolBox/ViewModels/ToolEditViewModel.cs
Normal file
191
PersonalToolBox/ViewModels/ToolEditViewModel.cs
Normal file
@@ -0,0 +1,191 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Input;
|
||||
using Microsoft.Win32;
|
||||
using PersonalToolBox.Models;
|
||||
using PersonalToolBox.Services;
|
||||
|
||||
namespace PersonalToolBox.ViewModels;
|
||||
|
||||
/// <summary>
|
||||
/// 工具编辑窗口 ViewModel,管理添加/编辑工具的交互逻辑
|
||||
/// </summary>
|
||||
public partial class ToolEditViewModel : ObservableObject
|
||||
{
|
||||
private readonly IDataService _dataService;
|
||||
private readonly ILogService _logService;
|
||||
private readonly ToolItem? _editingTool;
|
||||
|
||||
// ───────────────────────────── 可观察属性 ─────────────────────────────
|
||||
|
||||
[ObservableProperty]
|
||||
private string _windowTitle = "添加工具";
|
||||
|
||||
[ObservableProperty]
|
||||
private string _name = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _executablePath = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _arguments = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private string _hotKey = string.Empty;
|
||||
|
||||
[ObservableProperty]
|
||||
private Category? _selectedCategory;
|
||||
|
||||
/// <summary>
|
||||
/// 分类下拉列表
|
||||
/// </summary>
|
||||
public ObservableCollection<Category> Categories { get; } = new();
|
||||
|
||||
/// <summary>
|
||||
/// 窗口关闭回调(由 View 层设置)
|
||||
/// </summary>
|
||||
public Action<bool?>? CloseAction { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// 是否保存成功(供调用方判断是否需要刷新)
|
||||
/// </summary>
|
||||
public bool Saved { get; private set; }
|
||||
|
||||
// ───────────────────────────── 构造函数 ─────────────────────────────
|
||||
|
||||
public ToolEditViewModel(IDataService dataService, ILogService logService, ToolItem? toolToEdit = null)
|
||||
{
|
||||
_dataService = dataService;
|
||||
_logService = logService;
|
||||
_editingTool = toolToEdit;
|
||||
|
||||
// 加载分类列表
|
||||
foreach (var cat in _dataService.Config.Categories)
|
||||
Categories.Add(cat);
|
||||
|
||||
// 编辑模式
|
||||
if (toolToEdit != null)
|
||||
{
|
||||
WindowTitle = "编辑工具";
|
||||
Name = toolToEdit.Name;
|
||||
ExecutablePath = toolToEdit.ExecutablePath;
|
||||
Arguments = toolToEdit.Arguments;
|
||||
HotKey = toolToEdit.HotKey;
|
||||
SelectedCategory = Categories.FirstOrDefault(c => c.Id == toolToEdit.CategoryId);
|
||||
}
|
||||
}
|
||||
|
||||
// ───────────────────────────── 命令 ─────────────────────────────
|
||||
|
||||
/// <summary>
|
||||
/// 浏览本地文件
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void BrowseFile()
|
||||
{
|
||||
var dialog = new OpenFileDialog
|
||||
{
|
||||
Title = "选择可执行文件或脚本",
|
||||
Filter = "所有文件|*.*|可执行文件|*.exe|脚本文件|*.bat;*.cmd;*.ps1;*.py|快捷方式|*.lnk"
|
||||
};
|
||||
|
||||
if (dialog.ShowDialog() == true)
|
||||
{
|
||||
ExecutablePath = dialog.FileName;
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
{
|
||||
Name = Path.GetFileNameWithoutExtension(dialog.FileName);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 保存工具
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void Save()
|
||||
{
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Name))
|
||||
{
|
||||
_logService.Warning("工具名称不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
if (string.IsNullOrWhiteSpace(ExecutablePath))
|
||||
{
|
||||
_logService.Warning($"工具 \"{Name}\" 路径不能为空");
|
||||
return;
|
||||
}
|
||||
|
||||
// 编辑模式:更新已有工具
|
||||
if (_editingTool != null)
|
||||
{
|
||||
_editingTool.Name = Name.Trim();
|
||||
_editingTool.ExecutablePath = ExecutablePath.Trim();
|
||||
_editingTool.Arguments = Arguments.Trim();
|
||||
_editingTool.HotKey = HotKey.Trim();
|
||||
_editingTool.CategoryId = SelectedCategory?.Id ?? string.Empty;
|
||||
_editingTool.IsValid = IsExecutablePathValid(ExecutablePath.Trim());
|
||||
|
||||
_logService.Info($"已更新工具: {Name.Trim()}");
|
||||
}
|
||||
// 添加模式:创建新工具
|
||||
else
|
||||
{
|
||||
var newTool = new ToolItem
|
||||
{
|
||||
Name = Name.Trim(),
|
||||
ExecutablePath = ExecutablePath.Trim(),
|
||||
Arguments = Arguments.Trim(),
|
||||
HotKey = HotKey.Trim(),
|
||||
CategoryId = SelectedCategory?.Id ?? string.Empty,
|
||||
IsValid = IsExecutablePathValid(ExecutablePath.Trim())
|
||||
};
|
||||
|
||||
_dataService.Config.Tools.Add(newTool);
|
||||
_logService.Info($"已添加工具: {Name.Trim()}");
|
||||
}
|
||||
|
||||
_dataService.Save();
|
||||
Saved = true;
|
||||
CloseAction?.Invoke(true);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logService.Error($"保存工具失败: {ex.Message}");
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 取消编辑
|
||||
/// </summary>
|
||||
[RelayCommand]
|
||||
private void Cancel()
|
||||
{
|
||||
CloseAction?.Invoke(false);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 验证可执行路径是否有效
|
||||
/// </summary>
|
||||
private static bool IsExecutablePathValid(string path)
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(path))
|
||||
return false;
|
||||
|
||||
// URL 格式(https://...)直接通过
|
||||
if (Uri.TryCreate(path, UriKind.Absolute, out var uri) &&
|
||||
(uri.Scheme == Uri.UriSchemeHttp || uri.Scheme == Uri.UriSchemeHttps))
|
||||
return true;
|
||||
|
||||
// 纯文件名(无路径分隔符)可能位于系统 PATH 中
|
||||
if (!path.Contains('\\') && !path.Contains('/'))
|
||||
return true;
|
||||
|
||||
return File.Exists(path);
|
||||
}
|
||||
}
|
||||
@@ -4,10 +4,273 @@
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:local="clr-namespace:PersonalToolBox.Views"
|
||||
xmlns:vm="clr-namespace:PersonalToolBox.ViewModels"
|
||||
xmlns:models="clr-namespace:PersonalToolBox.Models"
|
||||
mc:Ignorable="d"
|
||||
Title="个人工具箱" Height="600" Width="900"
|
||||
WindowStartupLocation="CenterScreen">
|
||||
<Grid>
|
||||
Title="个人工具箱" Height="650" Width="960"
|
||||
WindowStartupLocation="CenterScreen"
|
||||
Background="{DynamicResource Theme.Background}">
|
||||
|
||||
<Window.Resources>
|
||||
<!-- 值转换器(必须定义在 DataTemplate 之前,确保 StaticResource 能解析) -->
|
||||
<local:BoolToOpacityConverter x:Key="BoolToOpacityConverter"/>
|
||||
<local:FirstCharConverter x:Key="FirstCharConverter"/>
|
||||
|
||||
<!-- 工具卡片模板 -->
|
||||
<DataTemplate x:Key="ToolCardTemplate" DataType="{x:Type models:ToolItem}">
|
||||
<Border Width="140" Height="100"
|
||||
Margin="6"
|
||||
Background="{DynamicResource Theme.CardBackground}"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="1"
|
||||
CornerRadius="8"
|
||||
Opacity="{Binding IsValid, Converter={StaticResource BoolToOpacityConverter}}"
|
||||
Cursor="Hand"
|
||||
ToolTip="{Binding Name}"
|
||||
Tag="{Binding DataContext, RelativeSource={RelativeSource AncestorType=Window}}">
|
||||
<Border.InputBindings>
|
||||
<MouseBinding Gesture="LeftDoubleClick"
|
||||
Command="{Binding DataContext.ExecuteToolCommand, RelativeSource={RelativeSource AncestorType=Window}}"
|
||||
CommandParameter="{Binding}"/>
|
||||
</Border.InputBindings>
|
||||
<Border.ContextMenu>
|
||||
<ContextMenu>
|
||||
<MenuItem Header="运行"
|
||||
Command="{Binding PlacementTarget.Tag.ExecuteToolCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
|
||||
CommandParameter="{Binding}"/>
|
||||
<MenuItem Header="编辑"
|
||||
Command="{Binding PlacementTarget.Tag.EditToolCommand, RelativeSource={RelativeSource AncestorType=ContextMenu}}"
|
||||
CommandParameter="{Binding}"/>
|
||||
</ContextMenu>
|
||||
</Border.ContextMenu>
|
||||
<StackPanel VerticalAlignment="Center" HorizontalAlignment="Center">
|
||||
<TextBlock Text="{Binding Name, Converter={StaticResource FirstCharConverter}}"
|
||||
FontSize="28"
|
||||
Foreground="{DynamicResource Theme.Accent}"
|
||||
HorizontalAlignment="Center"
|
||||
Margin="0,0,0,6"/>
|
||||
<TextBlock Text="{Binding Name}"
|
||||
FontSize="12"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
HorizontalAlignment="Center"
|
||||
TextTrimming="CharacterEllipsis"
|
||||
MaxWidth="120"/>
|
||||
</StackPanel>
|
||||
</Border>
|
||||
</DataTemplate>
|
||||
</Window.Resources>
|
||||
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="3"/>
|
||||
<RowDefinition Height="120"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<!-- ──────────────── 内容区 ──────────────── -->
|
||||
<Grid Grid.Row="0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="200"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<!-- ─── 左侧边栏 ─── -->
|
||||
<Border Grid.Column="0" Background="{DynamicResource Theme.SidebarBackground}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="60"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="45"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<TextBlock Grid.Row="0"
|
||||
Text="个人工具箱"
|
||||
FontSize="18" FontWeight="Bold"
|
||||
Foreground="{DynamicResource Theme.Accent}"
|
||||
VerticalAlignment="Center"
|
||||
HorizontalAlignment="Center"/>
|
||||
|
||||
<ListBox Grid.Row="1"
|
||||
ItemsSource="{Binding Categories}"
|
||||
SelectedItem="{Binding SelectedCategory}"
|
||||
Background="Transparent"
|
||||
BorderThickness="0"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
SelectionMode="Single">
|
||||
<ListBox.ItemContainerStyle>
|
||||
<Style TargetType="ListBoxItem">
|
||||
<Setter Property="Padding" Value="16,10"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="ListBoxItem">
|
||||
<Border x:Name="Border"
|
||||
Background="Transparent"
|
||||
BorderBrush="Transparent"
|
||||
BorderThickness="3,0,0,0"
|
||||
Padding="{TemplateBinding Padding}">
|
||||
<TextBlock Text="{Binding Name}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontSize="14"/>
|
||||
</Border>
|
||||
<ControlTemplate.Triggers>
|
||||
<Trigger Property="IsSelected" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#20FFFFFF"/>
|
||||
<Setter TargetName="Border" Property="BorderBrush" Value="{DynamicResource Theme.Accent}"/>
|
||||
</Trigger>
|
||||
<Trigger Property="IsMouseOver" Value="True">
|
||||
<Setter TargetName="Border" Property="Background" Value="#10FFFFFF"/>
|
||||
</Trigger>
|
||||
</ControlTemplate.Triggers>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ListBox.ItemContainerStyle>
|
||||
</ListBox>
|
||||
|
||||
<Button Grid.Row="2"
|
||||
Command="{Binding ToggleThemeCommand}"
|
||||
Background="Transparent"
|
||||
BorderThickness="1"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontSize="12"
|
||||
Height="35" Margin="10,0,10,10"
|
||||
Cursor="Hand">
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<TextBlock Text="◐" FontSize="16" Margin="0,0,8,0"
|
||||
VerticalAlignment="Center"/>
|
||||
<TextBlock Text="切换主题" VerticalAlignment="Center"/>
|
||||
</StackPanel>
|
||||
</Button>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<!-- ─── 右侧主体区 ─── -->
|
||||
<Grid Grid.Column="1">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="50"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Border Grid.Row="0" Background="{DynamicResource Theme.Background}"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="0,0,0,1">
|
||||
<Grid Margin="12,0">
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="100"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<TextBox Grid.Column="0"
|
||||
Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
BorderThickness="1"
|
||||
FontSize="14"
|
||||
VerticalContentAlignment="Center"
|
||||
Height="32"
|
||||
Margin="0,0,12,0"
|
||||
Padding="8,0"/>
|
||||
|
||||
<Button Grid.Column="1"
|
||||
Content="+ 添加工具"
|
||||
Command="{Binding AddToolCommand}"
|
||||
Background="{DynamicResource Theme.ButtonBackground}"
|
||||
Foreground="{DynamicResource Theme.ButtonForeground}"
|
||||
BorderThickness="0"
|
||||
FontSize="12"
|
||||
Height="32"
|
||||
Cursor="Hand">
|
||||
<Button.Resources>
|
||||
<Style TargetType="Border">
|
||||
<Setter Property="CornerRadius" Value="4"/>
|
||||
</Style>
|
||||
</Button.Resources>
|
||||
</Button>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<ScrollViewer Grid.Row="1"
|
||||
VerticalScrollBarVisibility="Auto"
|
||||
Background="{DynamicResource Theme.Background}">
|
||||
<ItemsControl ItemsSource="{Binding FilteredTools}"
|
||||
Margin="8">
|
||||
<ItemsControl.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<WrapPanel Orientation="Horizontal"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ItemsControl.ItemsPanel>
|
||||
<ItemsControl.ItemTemplate>
|
||||
<StaticResource ResourceKey="ToolCardTemplate"/>
|
||||
</ItemsControl.ItemTemplate>
|
||||
</ItemsControl>
|
||||
</ScrollViewer>
|
||||
</Grid>
|
||||
</Grid>
|
||||
|
||||
<!-- ──────────────── 分隔线 ──────────────── -->
|
||||
<Border Grid.Row="1" Background="{DynamicResource Theme.CardBorder}"/>
|
||||
|
||||
<!-- ──────────────── 底部日志栏 ──────────────── -->
|
||||
<Border Grid.Row="2" Background="{DynamicResource Theme.LogBackground}">
|
||||
<Grid>
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="30"/>
|
||||
<RowDefinition Height="*"/>
|
||||
</Grid.RowDefinitions>
|
||||
|
||||
<Border Grid.Row="0"
|
||||
Background="{DynamicResource Theme.SidebarBackground}"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="0,0,0,1">
|
||||
<Grid Margin="10,0">
|
||||
<TextBlock Text="运行日志"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontSize="12" FontWeight="SemiBold"
|
||||
VerticalAlignment="Center"/>
|
||||
<Button Content="清空日志"
|
||||
Command="{Binding ClearLogsCommand}"
|
||||
Background="Transparent"
|
||||
Foreground="{DynamicResource Theme.TextSecondary}"
|
||||
BorderThickness="0"
|
||||
FontSize="11"
|
||||
HorizontalAlignment="Right"
|
||||
Height="22"
|
||||
Cursor="Hand"/>
|
||||
</Grid>
|
||||
</Border>
|
||||
|
||||
<ListBox Grid.Row="1"
|
||||
x:Name="LogListBox"
|
||||
ItemsSource="{Binding Logs}"
|
||||
Background="Transparent"
|
||||
BorderThickness="0"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
FontFamily="Consolas"
|
||||
FontSize="12"
|
||||
VirtualizingPanel.IsVirtualizing="True">
|
||||
<ListBox.ItemContainerStyle>
|
||||
<Style TargetType="ListBoxItem">
|
||||
<Setter Property="Padding" Value="0,1,0,0"/>
|
||||
<Setter Property="Template">
|
||||
<Setter.Value>
|
||||
<ControlTemplate TargetType="ListBoxItem">
|
||||
<ContentPresenter/>
|
||||
</ControlTemplate>
|
||||
</Setter.Value>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ListBox.ItemContainerStyle>
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="{x:Type models:LogEntry}">
|
||||
<TextBlock Text="{Binding .}" Padding="8,1"
|
||||
Foreground="{DynamicResource Theme.Foreground}"/>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
</Grid>
|
||||
</Border>
|
||||
</Grid>
|
||||
</Window>
|
||||
|
||||
@@ -1,11 +1,63 @@
|
||||
using System.Windows;
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Globalization;
|
||||
using System.Windows;
|
||||
using System.Windows.Data;
|
||||
using PersonalToolBox.ViewModels;
|
||||
|
||||
namespace PersonalToolBox.Views;
|
||||
|
||||
public partial class MainWindow : Window
|
||||
{
|
||||
public MainWindow()
|
||||
private readonly MainViewModel _viewModel;
|
||||
|
||||
public MainWindow(MainViewModel viewModel)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_viewModel = viewModel;
|
||||
DataContext = viewModel;
|
||||
|
||||
viewModel.Logs.CollectionChanged += OnLogsCollectionChanged;
|
||||
}
|
||||
|
||||
private void OnLogsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs e)
|
||||
{
|
||||
if (e.Action == NotifyCollectionChangedAction.Add && LogListBox.Items.Count > 0)
|
||||
{
|
||||
Dispatcher.BeginInvoke(new Action(() =>
|
||||
{
|
||||
LogListBox.ScrollIntoView(LogListBox.Items[^1]);
|
||||
}), System.Windows.Threading.DispatcherPriority.Background);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// bool → double: true=1.0, false=0.4 (工具卡片失效时置灰)
|
||||
/// </summary>
|
||||
public class BoolToOpacityConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
return value is bool b && b ? 1.0 : 0.4;
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// string → string: 取字符串首字符作为图标占位
|
||||
/// </summary>
|
||||
public class FirstCharConverter : IValueConverter
|
||||
{
|
||||
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
{
|
||||
var s = value as string;
|
||||
return string.IsNullOrEmpty(s) ? "?" : s[0].ToString();
|
||||
}
|
||||
|
||||
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
|
||||
=> throw new NotImplementedException();
|
||||
}
|
||||
|
||||
126
PersonalToolBox/Views/ToolEditWindow.xaml
Normal file
126
PersonalToolBox/Views/ToolEditWindow.xaml
Normal file
@@ -0,0 +1,126 @@
|
||||
<Window x:Class="PersonalToolBox.Views.ToolEditWindow"
|
||||
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:models="clr-namespace:PersonalToolBox.Models"
|
||||
Title="{Binding WindowTitle}" Height="380" Width="480"
|
||||
WindowStartupLocation="CenterOwner"
|
||||
ResizeMode="NoResize"
|
||||
Background="{DynamicResource Theme.Background}">
|
||||
|
||||
<Grid Margin="20">
|
||||
<Grid.RowDefinitions>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
<RowDefinition Height="*"/>
|
||||
<RowDefinition Height="Auto"/>
|
||||
</Grid.RowDefinitions>
|
||||
<Grid.ColumnDefinitions>
|
||||
<ColumnDefinition Width="80"/>
|
||||
<ColumnDefinition Width="*"/>
|
||||
<ColumnDefinition Width="30"/>
|
||||
</Grid.ColumnDefinitions>
|
||||
|
||||
<!-- 工具名称 -->
|
||||
<TextBlock Grid.Row="0" Grid.Column="0"
|
||||
Text="名称:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<TextBox Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="6,0"/>
|
||||
|
||||
<!-- 工具路径 -->
|
||||
<TextBlock Grid.Row="1" Grid.Column="0"
|
||||
Text="路径:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<TextBox Grid.Row="1" Grid.Column="1"
|
||||
Text="{Binding ExecutablePath, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="6,0"/>
|
||||
<Button Grid.Row="1" Grid.Column="2"
|
||||
Content="..."
|
||||
Command="{Binding BrowseFileCommand}"
|
||||
Background="{DynamicResource Theme.ButtonBackground}"
|
||||
Foreground="{DynamicResource Theme.ButtonForeground}"
|
||||
BorderThickness="0"
|
||||
Width="24" Height="24" Margin="6,3,0,12"
|
||||
FontWeight="Bold" Cursor="Hand"/>
|
||||
|
||||
<!-- 运行参数 -->
|
||||
<TextBlock Grid.Row="2" Grid.Column="0"
|
||||
Text="参数:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<TextBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding Arguments, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="6,0"/>
|
||||
|
||||
<!-- 所属分类 -->
|
||||
<TextBlock Grid.Row="3" Grid.Column="0"
|
||||
Text="分类:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<ComboBox Grid.Row="3" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
ItemsSource="{Binding Categories}"
|
||||
SelectedItem="{Binding SelectedCategory}"
|
||||
DisplayMemberPath="Name"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="4,0"/>
|
||||
|
||||
<!-- 全局快捷键 -->
|
||||
<TextBlock Grid.Row="4" Grid.Column="0"
|
||||
Text="快捷键:" Foreground="{DynamicResource Theme.Foreground}"
|
||||
VerticalAlignment="Center" Margin="0,0,0,12"/>
|
||||
<TextBox Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="{Binding HotKey, UpdateSourceTrigger=PropertyChanged}"
|
||||
Background="{DynamicResource Theme.InputBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.InputBorder}"
|
||||
Height="30" Margin="0,0,0,12" Padding="6,0"/>
|
||||
|
||||
<!-- 提示文字 -->
|
||||
<TextBlock Grid.Row="5" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Text="快捷键格式示例: Ctrl+Alt+T"
|
||||
Foreground="{DynamicResource Theme.TextSecondary}"
|
||||
FontSize="11" Margin="0,0,0,16"/>
|
||||
|
||||
<!-- 分隔线 -->
|
||||
<Border Grid.Row="6" Grid.ColumnSpan="3"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="0,1,0,0" Margin="0,0,0,14"/>
|
||||
|
||||
<!-- 按钮 -->
|
||||
<StackPanel Grid.Row="7" Grid.Column="1" Grid.ColumnSpan="2"
|
||||
Orientation="Horizontal" HorizontalAlignment="Right">
|
||||
<Button Content="保存"
|
||||
Command="{Binding SaveCommand}"
|
||||
Background="{DynamicResource Theme.ButtonBackground}"
|
||||
Foreground="{DynamicResource Theme.ButtonForeground}"
|
||||
BorderThickness="0"
|
||||
Width="80" Height="30"
|
||||
FontSize="13"
|
||||
Cursor="Hand"
|
||||
Margin="0,0,10,0"/>
|
||||
<Button Content="取消"
|
||||
Command="{Binding CancelCommand}"
|
||||
Background="{DynamicResource Theme.CardBackground}"
|
||||
Foreground="{DynamicResource Theme.Foreground}"
|
||||
BorderBrush="{DynamicResource Theme.CardBorder}"
|
||||
BorderThickness="1"
|
||||
Width="80" Height="30"
|
||||
FontSize="13"
|
||||
Cursor="Hand"/>
|
||||
</StackPanel>
|
||||
</Grid>
|
||||
</Window>
|
||||
21
PersonalToolBox/Views/ToolEditWindow.xaml.cs
Normal file
21
PersonalToolBox/Views/ToolEditWindow.xaml.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System.Windows;
|
||||
using PersonalToolBox.ViewModels;
|
||||
|
||||
namespace PersonalToolBox.Views;
|
||||
|
||||
public partial class ToolEditWindow : Window
|
||||
{
|
||||
public ToolEditWindow(ToolEditViewModel viewModel)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
viewModel.CloseAction = (result) =>
|
||||
{
|
||||
// DialogResult 会自动关闭由 ShowDialog() 显示的窗口,无需显式调用 Close()
|
||||
DialogResult = result;
|
||||
};
|
||||
|
||||
Owner = Application.Current.MainWindow;
|
||||
DataContext = viewModel;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user