Table of Contents

View Models & Property Binding

How a generated model reads and writes the message buffer, what triggers a property-changed notification, and the string behaviour that catches everyone once.

Table of Contents

A Model Is a View Over Bytes

Reading and Writing

Change Detection

Strings Are Different

Arrays

Lifecycle Hooks

Publishing Parameters for Telemetry

Patterns That Work

A Model Is a View Over Bytes

A generated model holds no fields of its own. It holds a StoryboardMessage, and every property projects onto a range of that message's Data array:

ThermostatUpdate.Message.Data

 byte  0      2      4   5   6   7
      ┌──────┬──────┬───┬───┬───┬───┐
      │Target│Curr  │Ac │Fan│Tmr│Uni│
      └──────┴──────┴───┴───┴───┴───┘
       2s1    2s1    1u1 1u1 1u1 1u1

The offsets come from the generated metadata table, not from reflection — which is what keeps this AOT- and trimmer-safe.

One consequence worth stating early: the model and the wire format are the same thing. There is no serialisation step, no copy, and no moment where the two can disagree. Setting a property is writing the bytes that will be sent.

Reading and Writing

The generated properties are thin:

public UInt16 Percentage
{
    get { return OnGetValue<UInt16>(); }
    set { OnSetValue(value); }
}

OnGetValue and OnSetValue use [CallerMemberName] to find the field in the metadata, so the property name must match the metadata key — which it does, because both are generated from the same field name.

Method Behaviour
OnGetValue<T>() Reads the field from the buffer. Returns default if the name is not in the metadata.
OnSetValue<T>(value) Writes the field if the value differs, then raises property-changed

Reading a field whose name is not in the metadata returns default silently rather than throwing. This only happens if hand-written code calls OnGetValue with a name that is not a generated field.

Change Detection

OnSetValue compares the new value against what the buffer currently holds and does nothing if they are equal. No behaviors run, no notification is raised, and the buffer is untouched.

update.CurrentTemperature = 58;   // buffer written, PropertyChanged raised
update.CurrentTemperature = 58;   // nothing happens

This is usually what you want — it keeps a polling loop from flooding the UI with identical values. It does mean that "force a refresh by writing the current values again" does not work. To push the current state to a display that has just come up, send the message rather than rewriting the properties:

client.SendEvent(update.Message);   // sends whatever the buffer holds now

Strings Are Different

A string field is variable length. Writing one does not overwrite bytes in place — it allocates a new buffer and commits it back to the message.

That difference produces the most reported non-bug in this extension:

Warning

Two string values of the same length can appear not to update. Swapping "IDLE" for "STOP" may leave the display unchanged.

Practical consequences:

  • Choose status labels of different lengths. The MultiScreen demo's IDLE / RUN / SERVICE are deliberately 4, 3 and 7 characters.
  • Or pad to distinct lengths if the values are outside your control.
  • A string must be the last field in its event; see Events & the SBIO Format String.

Holding a reference to Message.Data across a string write gives you the old buffer — always read Message.Data fresh.

Arrays

A field with a count greater than one generates an array property:

public UInt16[] CylinderTemps
{
    get { return OnGetValue<UInt16[]>(); }
    set { OnSetValue(value); }
}

Arrays are fixed length, written element by element at startByte + i * elementSize. Two things to know:

  • The getter returns a fresh array each call, not a view. Mutating it does not write to the buffer — assign the whole array back.
  • Change detection compares array references, not contents, so assigning a different array object always writes even if the values are identical.
var temps = update.CylinderTemps;   // copy
temps[0] = 91;                      // does NOT reach the buffer
update.CylinderTemps = temps;       // this does

Lifecycle Hooks

Hook When Typical use
OnInit() After the message buffer exists, for both constructors Stamp Message.ChannelId; seed defaults
OnPropertyChanged(name) After a property actually changes Inherited from ViewModelBase
OnGetPropertyMap(parms) When the model publishes itself for telemetry Overridden for you; see below

OnInit is the important one, because it runs for both the "about to send" constructor and the "just received" constructor. Anything that must always hold about a model belongs there:

public partial class DashboardUpdate
{
    protected override void OnInit()
    {
        Message.ChannelId = MultiScreenChannels.DashboardChannel;
    }
}

Publishing Parameters for Telemetry

StoryboardViewModelBase overrides OnGetPropertyMap to publish every field as a system parameter, so Storyboard values appear alongside other OpenPV parameters:

Parameter attribute Source
Name <ModelType>_<FieldName>
System unique id The field's UniqueId from the generated metadata
Value type Derived from the field's SBFieldType
Default value The field's current value

The unique id is generated per field and stable across builds, which is what lets a recorded parameter keep its identity as the application evolves.

Separately, the Toolkit reads custom.evt directly at design time to offer Storyboard fields as parameters without running your application — which is why the Toolkit can show them before the project has ever been built.

Patterns That Work

Keep the model authoritative, not the UI. The UI cannot be queried, so treat inbound events as requests and outbound events as the truth:

public partial class ChangeBrightness          // inbound: a request
{
    public override void OnUpdateModel(string target)
    {
        var next = Increase == 1 ? Model.Percentage + Value : Model.Percentage - Value;
        Model.SetBrightness(Math.Clamp(next, 5, 100));   // model decides
    }
}

Push state, do not assume it. After a screen restarts, the engine knows nothing. Send a full snapshot on connect rather than relying on incremental updates.

Group related fields into one event. Each SendEvent is a separate SBIO write; a single ThermostatUpdate carrying six fields beats six single-field events, and arrives atomically.

Stamp ChannelId once, in OnInit. Setting it at each call site is how updates end up on the wrong display.

Next: Packaging & Launch.