diff --git a/STranslate/MainWindow.xaml.cs b/STranslate/MainWindow.xaml.cs index d11a7af..b838067 100644 --- a/STranslate/MainWindow.xaml.cs +++ b/STranslate/MainWindow.xaml.cs @@ -287,6 +287,7 @@ namespace STranslate /// private void Exit_Click(object sender, EventArgs e) { + vm.Dispose(); notifyIcon.Dispose(); Environment.Exit(0); } diff --git a/STranslate/Model/ConfigModel.cs b/STranslate/Model/ConfigModel.cs index de9a49d..113701c 100644 --- a/STranslate/Model/ConfigModel.cs +++ b/STranslate/Model/ConfigModel.cs @@ -18,8 +18,8 @@ namespace STranslate.Model return new ConfigModel { IsBright = true, - SourceLanguage = LanguageEnum.AUTO, - TargetLanguage = LanguageEnum.AUTO, + SourceLanguage = LanguageEnum.AUTO.GetDescription(), + TargetLanguage = LanguageEnum.AUTO.GetDescription(), SelectServer = 0, Servers = new Server[] { @@ -49,10 +49,10 @@ namespace STranslate.Model public bool IsBright { get; set; } [JsonProperty("sourceLanguage")] - public LanguageEnum SourceLanguage { get; set; } + public string SourceLanguage { get; set; } [JsonProperty("targetLanguage")] - public LanguageEnum TargetLanguage { get; set; } + public string TargetLanguage { get; set; } [JsonProperty("selectServer")] public int SelectServer { get; set; } diff --git a/STranslate/ViewModel/MainVM.cs b/STranslate/ViewModel/MainVM.cs index fec6a84..c1a6696 100644 --- a/STranslate/ViewModel/MainVM.cs +++ b/STranslate/ViewModel/MainVM.cs @@ -14,12 +14,12 @@ using STranslate.Util; namespace STranslate.ViewModel { - public class MainVM : BaseVM + public class MainVM : BaseVM, IDisposable { public MainVM() { - if (!InitialConfig()) + if (!ReadConfig()) { Task.Delay(3000); Environment.Exit(-1); @@ -75,7 +75,7 @@ namespace STranslate.ViewModel /// 初始化配置文件 /// /// - private bool InitialConfig() + private bool ReadConfig() { try { @@ -98,8 +98,8 @@ namespace STranslate.ViewModel } //从配置读取source target - InputComboSelected = GlobalConfig.SourceLanguage.GetDescription(); - OutputComboSelected = GlobalConfig.TargetLanguage.GetDescription(); + InputComboSelected = GlobalConfig.SourceLanguage; + OutputComboSelected = GlobalConfig.TargetLanguage; return true; } @@ -109,6 +109,25 @@ namespace STranslate.ViewModel return false; } } + private void WriteConfig() + { + try + { + ConfigHelper.Instance.WriteConfig(new ConfigModel + { + IsBright = true, + SourceLanguage = InputComboSelected, + TargetLanguage = OutputComboSelected, + SelectServer = TranslationInterface.FindIndex(x => x == SelectedTranslationInterface), + Servers = GlobalConfig.Servers, + }); + } + catch (Exception ex) + { + MessageBox.Show(ex.Message, "错误", MessageBoxButton.OK, MessageBoxImage.Error); + } + } + /// /// 自动识别语种 /// @@ -206,6 +225,11 @@ namespace STranslate.ViewModel OutputTxt = ex.Message; } } + + public void Dispose() + { + WriteConfig(); + } #endregion handle #region Params