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

Alternative approach to attached properties using CaptureUnmatchedValues #5

Closed
Dreamescaper opened this issue May 13, 2022 · 0 comments

Comments

@Dreamescaper
Copy link
Owner

Since Blazor does not support attached properties natively (see dotnet/aspnetcore#20715, dotnet/aspnetcore#5607, dotnet/aspnetcore#22316), we have to emulate them somehow.
Currently we use the approach with separate Blazor components - either parent (e.g. GridCell) or child (e.g. ShellProperties). However, sometimes it is quite cumbersome and doesn't feel natural.

I have an alternative suggestion.

Create some AttachedPropertyRegistry class, which would allow to register actions for certain attached properties, and register them in corresponding element's static constructor. E.g. for Grid it would look smth like that:

            AttachedPropertyRegistry.RegisterAttachedPropertyHandler("Grid.Row",
                (element, value) => GridLayout.SetRow(element, Convert.ToInt32(value, CultureInfo.InvariantCulture)));

Add property to Element, which would capture all unmatched values:

        [Parameter(CaptureUnmatchedValues = true)] public Dictionary<string, object> AdditionalProperties { get; set; }

And in ElementHandler try to retrieve and execute action from AttachedPropertyRegistry for all unmatched values.

This approach would allow to use syntax, very close to what is used in XAML:

<Grid ColumnDefinitions="*,*,*" RowDefinitions="*,*,*">
	<Label Grid.Row="0" Grid.Column="0" Text="1" />
	<Label Grid.Row="1" Grid.Column="1" Text="2" />
	<Label Grid.Row="2" Grid.Column="2" Text="3" />
</Grid>

Of course, this approach is not perfect either.
Because this property is a simple Dictionary, there would be no IntelliSense suggestions or compile time validations.
Besides, we probably won't be able to handle complex properties this way, like

<ContentPage ...>    
    <Shell.BackButtonBehavior>
        <BackButtonBehavior Command="{Binding BackCommand}"
                            IconOverride="back.png" />   
    </Shell.BackButtonBehavior>
    ...
</ContentPage>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant