using System.Windows; using SWS.Commons; using SWS.Service; namespace DI_Electrical.ViewModels { public class ConfigViewModel : DialogBase { private string address; public string Address { get { return this.address; } set { if (value != this.address) { this.address = value; RaisePropertyChanged(nameof(Address)); } } } private int port; public int Port { get { return this.port; } set { if (value != this.port) { this.port = value; RaisePropertyChanged(nameof(Port)); } } } private string _directory; public string Directory { get { return this._directory; } set { if (value != this._directory) { this._directory = value; RaisePropertyChanged(nameof(Directory)); } } } ConfigService _configService; LoginService _LoginService; public ConfigViewModel(ConfigService configService, LoginService loginService) : base() { title = "设置"; _configService = configService; _LoginService = loginService; _configService.Read(out address, out port, out _directory); } public override async void ExecuteOKCommandAsync(object obj) { if (string.IsNullOrEmpty(Directory)) { MessageBox.Show("本地文件目录不能为空。"); return; } if (!System.IO.Directory.Exists(Directory)) { MessageBox.Show("本地文件目录不存在。"); return; } //var data = _configService.parser.ReadFile(_configService.path); //_configService._httpService.Init(address, port); IsBusy = true; BusyContent = "保存中..."; var testRes = await _LoginService.Login("1", "11"); IsBusy = false; if (testRes.StartsWith("http错误。")) { MessageBox.Show("无法连接上服务器。请检查服务器 和 端口。"); return; } if (address.StartsWith("http")) { address = address.Replace("https://", "").Replace("http://", ""); } //data["Profile"]["Address"] = address; //data["Profile"]["Port"] = port.ToString(); //data["Profile"]["Directory"] = _directory; //_configService.parser.WriteFile(_configService.path, data); //IniHelper.WriteValueFromIniFile("Profile", "strIPAddress", address); //IniHelper.WriteValueFromIniFile("Profile", "nPort", port.ToString()); //IniHelper.WriteValueFromIniFile("Profile", "strLocalWorkDir", _directory); _configService.Save("Profile", "strIPAddress", address); _configService.Save("Profile", "nPort", port.ToString()); _configService.Save("Profile", "strLocalWorkDir", _directory); GlobalObject.isConfigIniCreateBySys = false; GlobalObject.SetLocalFileDirectory(_directory); CloseWindowAction?.Invoke();//关闭 } } }