Skip to content

ViewModelInstance list items don't have callbacks bound correctly #142

@evorowy-jk

Description

@evorowy-jk

Hi, when working with ViewModelInstances located in a List property, property callbacks (like OnTriggered) are not being executed, even though the triggers themselves fire correctly.

Steps to Reproduce

Create a ViewModel with a list property containing child ViewModels:
Image

Create a child ViewModel with a trigger property:
Image

Subscribe to the trigger callback in code:

for (var index = 0; index < _buttons.Count; index++)
{
    var buttonInstance = _buttons.GetInstanceAt(index);
                
    var onClickTrigger = buttonInstance.GetTriggerProperty("onClicked");
    onClickTrigger.OnTriggered += () => Debug.Log("Button clicked");
}

Expected Behaviour

The OnTriggered callback should execute when the trigger fires.

Actual Behaviour

The onClicked trigger fires correctly, but the OnTriggered callback is never executed.

Root Cause

In ViewModelInstanceListProperty.cs, the method GetOrCreateVMInstanceFromPtr does not pass RootInstance to GetOrCreateFromPointer, which prevents proper child ↔ parent binding.

Current code:

private ViewModelInstance GetOrCreateVMInstanceFromPtr(IntPtr instancePtr)
{
    if (instancePtr == IntPtr.Zero)
    {
        return null;
    }
    return ViewModelInstance.GetOrCreateFromPointer(instancePtr, this.RootInstance?.RiveFile);
}

Proposed Solution

Pass this.RootInstance as the third parameter:

private ViewModelInstance GetOrCreateVMInstanceFromPtr(IntPtr instancePtr)
{
    if (instancePtr == IntPtr.Zero)
    {
        return null;
    }
    return ViewModelInstance.GetOrCreateFromPointer(instancePtr, this.RootInstance?.RiveFile, this.RootInstance);
}

Cheers and keep up the good work!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions