diff --git a/src/STranslate.Model/ConfigModel.cs b/src/STranslate.Model/ConfigModel.cs index 219c5ba0..67f182db 100644 --- a/src/STranslate.Model/ConfigModel.cs +++ b/src/STranslate.Model/ConfigModel.cs @@ -567,7 +567,7 @@ public ConfigModel Clone() SourceLangIfAuto = SourceLangIfAuto, TargetLangIfSourceZh = TargetLangIfSourceZh, TargetLangIfSourceNotZh = TargetLangIfSourceNotZh, - ReplaceProp = (ReplaceProp)ReplaceProp.Clone(), + ReplaceProp = ReplaceProp.Clone(), Hotkeys = Hotkeys?.Clone(), Services = Services?.Clone(), OCRList = OCRList?.DeepCopy(), diff --git a/src/STranslate.Model/ReplaceProp.cs b/src/STranslate.Model/ReplaceProp.cs index 0e7c310c..762c19bf 100644 --- a/src/STranslate.Model/ReplaceProp.cs +++ b/src/STranslate.Model/ReplaceProp.cs @@ -2,7 +2,7 @@ namespace STranslate.Model; -public partial class ReplaceProp : ObservableObject, ICloneable +public partial class ReplaceProp : ObservableObject { [ObservableProperty] private ITranslator? _activeService; @@ -33,7 +33,7 @@ public partial class ReplaceProp : ObservableObject, ICloneable /// [ObservableProperty] private LangEnum _targetLangIfSourceNotZh = LangEnum.zh_cn; - public object Clone() + public ReplaceProp Clone() { return new ReplaceProp { diff --git a/src/STranslate/ViewModels/Preference/ReplaceViewModel.cs b/src/STranslate/ViewModels/Preference/ReplaceViewModel.cs index 59e88223..815d34f3 100644 --- a/src/STranslate/ViewModels/Preference/ReplaceViewModel.cs +++ b/src/STranslate/ViewModels/Preference/ReplaceViewModel.cs @@ -14,19 +14,28 @@ public partial class ReplaceViewModel : ObservableObject private readonly ConfigHelper _configHelper = Singleton.Instance; private readonly TranslatorViewModel _translateVm = Singleton.Instance; public InputViewModel InputVm => Singleton.Instance; + private Guid? deletedSvc; public ReplaceViewModel() { // View 上绑定结果从List中获取 ReplaceProp.ActiveService = AllServices.FirstOrDefault(x => x.Identify == ReplaceProp.ActiveService?.Identify); - _translateVm.PropertyChanged += (sender, args) => + _translateVm.CurTransServiceList.ListChanged += (sender, args) => { - // 检查是否被删除 - if (sender is not BindingList services || ReplaceProp.ActiveService is null) return; - - // 当服务列表中有增删时检查当前活动服务是否被删除 - if (services.All(x => x.Identify != ReplaceProp.ActiveService?.Identify)) - ReplaceProp.ActiveService = null; + switch (args.ListChangedType) + { + case ListChangedType.ItemAdded: + var svc = _translateVm.CurTransServiceList[args.NewIndex]; + if (svc.Identify == deletedSvc) + { + ReplaceProp.ActiveService = svc; + deletedSvc = null; + } + break; + case ListChangedType.ItemDeleted: + deletedSvc = ReplaceProp.ActiveService?.Identify; + break; + } }; } @@ -140,7 +149,7 @@ private async Task TranslateLlmAsync(RequestModel req, CancellationToken token) #region Property - [ObservableProperty] private BindingList _allServices = Singleton.Instance.CurTransServiceList.Clone(); + [ObservableProperty] private BindingList _allServices = Singleton.Instance.CurTransServiceList; [ObservableProperty] private ReplaceProp _replaceProp = Singleton.Instance.CurrentConfig?.ReplaceProp ?? new ReplaceProp();