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 / Hex | ASCII 或十六进制 |
控件已销毁或尚未创建句柄时,更新会被丢弃。
常见问题
- 出现跨线程异常:请使用本程序集的
BindTo,不要直接在DataReceived里改控件。 - 关闭窗口后串口仍被占用:使用
AttachZeus,不要自己Start却忘记释放。 - 要绑定
Text以外的属性:使用channel.BindText(dispatcher, text => myControl.Value = text)。