Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No calls to OnXXXChanged event handlers on Avalonia.StyledProperty #12

Closed
graealex opened this issue Mar 26, 2023 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@graealex
Copy link

Describe the bug

When creating a StyledProperty via for example:

[DependencyProperty<int>("SelectedType")]

The resulting generated code does never call the OnSelectedTypeChanged(...) event handler, subsequently no event notifications when the value changes are received.

The same property as a DirectProperty:

[DependencyProperty<int>("SelectedType", IsDirect = true)]

contains the necessary calls on all versions of the OnSelectedTypeChanged(...) event handler in the property setter.

I understand that event handlers for value changes have become a bit cumbersome, as the notifying parameter has become internal (as of Avalonia 11 preview 6), and as such is not available anymore. The preferred route to receive event notifications is to either override the OnPropertyChanged method, or attach to the PropertyChange event. (as per Max Katz from AvaloniaUI core developer team)

Steps to reproduce the bug

Create StyledProperty in an Avalonia project and try to listen for value changes, which will not fire.

Expected behavior

No response

Screenshots

No response

NuGet package version

1.2.5

IDE

Visual Studio 2022

Additional context

No response

@graealex graealex added the bug Something isn't working label Mar 26, 2023
@HavenDV
Copy link
Owner

HavenDV commented Mar 26, 2023

//HintName: MyControl.StaticConstructor.generated.cs

using System;

#nullable enable

namespace H.Generators.IntegrationTests
{
    public partial class MyControl
    {
        static MyControl()
        {
            IsSpinningProperty.Changed.Subscribe(static x =>
            {
                ((global::H.Generators.IntegrationTests.MyControl)x.Sender).OnIsSpinningChanged(
                    (bool)x.OldValue.GetValueOrDefault(),
                    (bool)x.NewValue.GetValueOrDefault());
            });
        }
    }
}

In addition to the main properties file for Avalonia, a static constructor is also generated that calls the OnChanged methods. Please let me know if there are any problems with this.

@HavenDV
Copy link
Owner

HavenDV commented Mar 26, 2023

Yes, I added integration tests for this case and it doesn't really call the methods. I will research this

@graealex
Copy link
Author

Thanks for the fast response - Preview 5 and 6 broke a lot of things. For now the workaround is to manually override OnPropertyChanged, but that's certainly not particularly elegant. Or maybe they unintentionally broke the Subscribe method as well.

@HavenDV
Copy link
Owner

HavenDV commented Mar 26, 2023

Yes, they changed the Subscribe method and now it does not contain data about the sender, old and new value. But the option via overriding OnPropertyChanged will not work for attached properties.
So I think that now it cannot be bypassed and we need to wait for a stable version. I rechecked and everything definitely works for stable versions of Avalonia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants