using HMI.Core.Util; using HMI.UI.DpUtil; using HMI.UI.Util; using HMI.Util; using Panuon.WPF.UI; using Plugin.TestResult.Model; using Plugin.TestResult_WPF; using System; using System.ComponentModel; using System.IO.Ports; namespace Plugin.TestResult { /// /// ScannerSetting.xaml 的交互逻辑 /// public partial class ScannerSetting : WindowX { public ScannerSetting() { DataContext = this; InitializeComponent(); } public ScannerConfig Data { get; set; } private bool _isProcessing; protected override void OnClosing(CancelEventArgs e) { //Press Continue Button if (DialogResult == true) { if (_isProcessing) { e.Cancel = true; return; } var okButton = WindowXModalDialog.GetOKButton(this); try { _isProcessing = true; ButtonHelper.SetIsPending(okButton, true); if (!ValidateForm()) { e.Cancel = true; return; } SaveOrUpdateData(); } catch (Exception ex) { ToastHelper.Error(this, $"Error!{ex.Message}"); e.Cancel = true; return; } finally { _isProcessing = false; ButtonHelper.SetIsPending(okButton, false); } } base.OnClosing(e); } private bool ValidateForm() { bool flag = FormGroupHelper.ValidateForm(formPanel); if (flag) { Data.PortName = CbPortName.SelectedItem?.ToString(); Data.BaudRate = (int)(NiBaudRate.Value??0); Data.DataBits = (int)(NiDataBits.Value??0); Data.StopBits = (StopBits)CbStopBits.SelectedItem; Data.DtrEnable = CxDtrEnable.IsChecked ?? false; Data.RtsEnable = CxRtsEnable.IsChecked ?? false; } return flag; } private bool _isEnabled; public bool SbIsEnabled { get => _isEnabled; set { Set(ref _isEnabled, value); } } private string section; public bool? Open(bool showPortName, string section) { string[] ports = SerialPort.GetPortNames(); CbPortName.ItemsSource = ports; this.section = section; this.Title = section == "Config" ? this.lang("Tr.CommonSetting") : (section + this.lang("Tr.ScannerSetting")); if (showPortName) { FgPortName.Visibility = System.Windows.Visibility.Visible; FormGroupHelper.SetIsRequired(FgPortName, true); SbIsEnabled = false; } else { FgPortName.Visibility = System.Windows.Visibility.Collapsed; FormGroupHelper.SetIsRequired(FgPortName, false); SbIsEnabled = true; } var data = ScannerConfig.ReadFromIni(ViewControl.SannerIniFile, "Config"); string portName = IniFileHelper.ReadIniData(section, "PortName", "", ViewControl.SannerIniFile); if (string.IsNullOrEmpty(portName)) { if (section.StartsWith("A")) { portName = "COM1"; } else { portName = "COM2"; } } data.PortName = portName; Data = data; CbPortName.SelectedItem = Data.PortName; NiBaudRate.Value = Data.BaudRate; NiDataBits.Value = Data.DataBits; CbStopBits.SelectedItem = Data.StopBits; CxDtrEnable.IsChecked = Data.DtrEnable; CxRtsEnable.IsChecked = Data.RtsEnable; return this.ShowDialog(); } private void SaveOrUpdateData() { string iniFile = ViewControl.SannerIniFile; IniFileHelper.WriteIniData(section, "PortName", Data.PortName, iniFile); if (section == "Config") { IniFileHelper.WriteIniData(section, "BaudRate", Data.BaudRate.ToString(), iniFile); IniFileHelper.WriteIniData(section, "DataBits", Data.DataBits.ToString(), iniFile); IniFileHelper.WriteIniData(section, "StopBits", Data.StopBits.ToString(), iniFile); IniFileHelper.WriteIniData(section, "DtrEnable", Data.DtrEnable.ToString(), iniFile); IniFileHelper.WriteIniData(section, "RtsEnable", Data.RtsEnable.ToString(), iniFile); } } } }