using Advantech.Motion; using Newtonsoft.Json; using AssistClient.Device; using AssistClient.UI.PropExtend; using AssistClient.Utils; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Forms; using System.Xml.Linq; namespace AssistClient.UI { public partial class UICodeScannerDev : UserControl { SynchronizationContext SyncContext = null; // public class CodeScannerProp { [PropertyOrder(1), Browsable(true), Category("数据"), DisplayName("Code"), Description("Code"), ReadOnly(true), JsonIgnore] public string code { get; set; } public string serialize() { return JsonConvert.SerializeObject(this); } } private CodeScannerProp prop = new CodeScannerProp(); private CodeScannerDev dev; public UICodeScannerDev() { InitializeComponent(); propertyGrid1.PropertyValueChanged += propertyGrid1_PropertyValueChanged; //this.toolStrip1.Enabled = false; this.propertyGrid1.Enabled = false; //获取UI线程同步上下文 SyncContext = SynchronizationContext.Current; init(); } //public string getParamsData() //{ // return prop.serialize(); //} //public void setParamsData(string json) //{ //if (json == "") return; // prop.deserialize(json); // this.propertyGrid1.Refresh(); //} public void init() { this.propertyGrid1.SelectedObject = prop; dev = new CodeScannerDev(); dev.WarningEvent = (level, info) => { txtLog.Text = $"({level}){info}"; }; //IN dev.ScanerEvent += new System.Action((data) => { prop.code = data; this.refreshUI(); }); if (!dev.start()) { this.closeDev(); return; } //this.toolStrip1.Enabled = true; this.propertyGrid1.Enabled = true; } private void refreshUI() { SyncContext.Post(m => { var result = m as string; propertyGrid1.Refresh(); //tbtnJogOnOff.Text = (prop.AxState == AxisState.STA_AX_EXT_JOG) ? "关闭Jog" : "开启Jog"; //tbtnLeft.Enabled = tbtnRight.Enabled = (prop.AxState == AxisState.STA_AX_EXT_JOG); }, "异步操作完成结果"); } private void closeDev() { try { if (dev == null) return; dev.stop(); } catch (Exception ex) { //this.toolStrip1.Enabled = false; this.propertyGrid1.Enabled = false; MessageBox.Show(ex.Message, "失败", MessageBoxButtons.OK, MessageBoxIcon.Error); } } protected override void OnHandleDestroyed(EventArgs e) { base.OnHandleDestroyed(e); // 在此添加需要手动释放资源的代码 this.closeDev(); } private void propertyGrid1_PropertyValueChanged(object s, PropertyValueChangedEventArgs e) { //其中包含了两个重要的属性:OldValue和ChangeItem。 //ChangeItem是“PropertyDescriptorGridEntry”类型。一般可以通过ChangeItem.PropertyDescriptor.ComponentType查找到属性的实际类型。 //而通过ChangeItem的Label,则可以查看到当前在属性编辑输入框中显示的值。 string className = e.ChangedItem.PropertyDescriptor.ComponentType.Name; string propertyName = e.ChangedItem.PropertyDescriptor.Name; var oldValue = e.OldValue; var newValue = e.ChangedItem.Value; switch (propertyName) { case "xxx": //refreshAxisVelParam(); break; } } } }