跳到主要内容

WinForms 适配器

把通道事件切到 UI 线程,并提供控件绑定,避免每个窗体手写 InvokeRequired

var attachment = this.AttachZeus(builder => builder.AddVirtualChannel("meter"));
var meter = attachment.Host.Channels.Get("meter");
meter.BindState(stateLabel);
meter.BindTo(echoLabel);
meter.BindTo(hexLabel, ChannelTextFormatter.Hex);

程序集:Zeus.Presentation.WinForms(目标框架 net8.0-windows)。

API

API作用
Form.AttachZeus(...)Load 时启动,FormClosed 时释放
BindTo / BindText把数据写到 Control.Text
BindState显示通道状态
AsBindingSource(control)INotifyPropertyChanged 投影
points.BindTo("temperature", label)把点表值写到控件
ChannelTextFormatter.Default / HexASCII 或十六进制

控件已销毁或尚未创建句柄时,更新会被丢弃。

常见问题

  • 出现跨线程异常:请使用本程序集的 BindTo,不要直接在 DataReceived 里改控件。
  • 关闭窗口后串口仍被占用:使用 AttachZeus,不要自己 Start 却忘记释放。
  • 要绑定 Text 以外的属性:使用 channel.BindText(dispatcher, text => myControl.Value = text)