WinForms Adapter
The WinForms adapter marshals channel events and point changes to the UI thread and provides control binding helpers so each form does not need custom InvokeRequired code.
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);
meter.BindEnabled(sendButton);
Assembly: Zeus.Presentation.WinForms with target framework net8.0-windows.
APIs
| API | Purpose |
|---|---|
Form.AttachZeus(...) | Starts on load and disposes on form close |
BindTo / BindText | Writes received data to Control.Text |
BindState | Displays channel state |
BindEnabled | Sets Control.Enabled, defaulting to enabled only when Open |
AsBindingSource(control) | Exposes an INotifyPropertyChanged projection |
points.BindTo("temperature", label) | Writes point value to a control |
points.BindSnapshot(...) | Pushes full point snapshots to the UI thread |
points.BindHistory(...) | Pushes recent successful samples to the UI thread |
points.BindAlarmBackColor / BindAlarmForeColor | Changes colors based on alarm/error state |
points.BindWriteBack | Binds a button click to Points.WriteAsync |
Destroyed controls or controls without handles ignore updates.
Point Binding
points.BindTo("temperature", temperatureLabel, value => $"{value:0.0} C");
points.BindAlarmBackColor("temperature", temperatureLabel);
points.BindSnapshot("temperature", alarmLabel, snapshot =>
{
alarmLabel.Text = snapshot.AlarmState.ToString();
});
points.BindEnabled("setpoint", writeButton);
points.BindWriteBack(
"setpoint",
setpointTextBox,
writeButton,
text => double.Parse(text, CultureInfo.InvariantCulture),
ex => MessageBox.Show(this, ex.Message, "Write failed", MessageBoxButtons.OK, MessageBoxIcon.Warning));
If you already use WinForms data binding, bind the projection:
var source = points.AsBindingSource("temperature", this);
temperatureLabel.DataBindings.Add("Text", source, nameof(PointBindingSource.Text));
alarmLabel.DataBindings.Add("Text", source, nameof(PointBindingSource.AlarmState));
Common Issues
- Cross-thread exception: use this package's
BindToinstead of updating controls insideDataReceived. - Serial port remains busy after closing: use
AttachZeusor dispose the host yourself. - Need to bind another property: use a specific helper when available, or map it yourself through
BindSnapshot.