From a66b802bbd76ce9363bf8b7b9b4e0e7b8671c526 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BE=89=E9=B8=AD=E8=9B=8B?= Date: Sun, 29 Dec 2024 19:00:26 +0800 Subject: [PATCH] fixed #661 --- .../Model/KeyMouseScriptItem.cs | 2 ++ .../View/Pages/KeyMouseRecordPage.xaml | 2 +- .../Pages/KeyMouseRecordPageViewModel.cs | 18 ++++++++++-------- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/BetterGenshinImpact/Model/KeyMouseScriptItem.cs b/BetterGenshinImpact/Model/KeyMouseScriptItem.cs index 7e4dcfc4..c1a9f6bb 100644 --- a/BetterGenshinImpact/Model/KeyMouseScriptItem.cs +++ b/BetterGenshinImpact/Model/KeyMouseScriptItem.cs @@ -12,4 +12,6 @@ public partial class KeyMouseScriptItem : ObservableObject private string _createTimeStr = string.Empty; public DateTime CreateTime { get; set; } + + public string Path { get; set; } = string.Empty; } diff --git a/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml b/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml index b3ef62ed..0ee39be0 100644 --- a/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml +++ b/BetterGenshinImpact/View/Pages/KeyMouseRecordPage.xaml @@ -106,7 +106,7 @@ diff --git a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs index ae24f45e..fb1532b4 100644 --- a/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs +++ b/BetterGenshinImpact/ViewModel/Pages/KeyMouseRecordPageViewModel.cs @@ -45,15 +45,16 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation private void InitScriptListViewData() { - _scriptItems.Clear(); + ScriptItems.Clear(); var fileInfos = LoadScriptFiles(scriptPath) .OrderByDescending(f => f.CreationTime) .ToList(); foreach (var f in fileInfos) { - _scriptItems.Add(new KeyMouseScriptItem + ScriptItems.Add(new KeyMouseScriptItem { Name = f.Name, + Path = f.FullName, CreateTime = f.CreationTime, CreateTimeStr = f.CreationTime.ToString("yyyy-MM-dd HH:mm:ss") }); @@ -67,7 +68,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation Directory.CreateDirectory(folder); } - var files = Directory.GetFiles(folder, "*.*", + var files = Directory.GetFiles(folder, "*.json", SearchOption.AllDirectories); return files.Select(file => new FileInfo(file)).ToList(); @@ -120,12 +121,13 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation } [RelayCommand] - public async Task OnStartPlay(string name) + public async Task OnStartPlay(string path) { + string name = Path.GetFileName(path); _logger.LogInformation("重放开始:{Name}", name); try { - var s = await File.ReadAllTextAsync(Path.Combine(scriptPath, name)); + var s = await File.ReadAllTextAsync(path); await new TaskRunner(DispatcherTimerOperationEnum.UseSelfCaptureImage) .RunAsync(async () => await KeyMouseMacroPlayer.PlayMacro(s, CancellationContext.Instance.Cts.Token)); @@ -159,11 +161,11 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation if (!string.IsNullOrEmpty(str)) { // 检查原始文件是否存在 - var originalFilePath = Path.Combine(scriptPath, item.Name); + var originalFilePath = item.Path; if (File.Exists(originalFilePath)) { // 重命名文件 - File.Move(originalFilePath, Path.Combine(scriptPath, str + ".json")); + File.Move(originalFilePath, Path.Combine(Path.GetDirectoryName(originalFilePath)!, str + ".json")); _snackbarService.Show( "修改名称成功", $"脚本名称 {item.Name} 修改为 {str}", @@ -199,7 +201,7 @@ public partial class KeyMouseRecordPageViewModel : ObservableObject, INavigation } try { - File.Delete(Path.Combine(scriptPath, item.Name)); + File.Delete(item.Path); _snackbarService.Show( "删除成功", $"{item.Name} 已经被删除",