CommunityToolkit/Maui.Markup

[Proposal] Behaviors extension to VisualElementExtensions

bijington opened this issue ยท 6 comments

Behaviors extension to VisualElementExtensions

  • Proposed
  • Prototype
  • Implementation
    • iOS Support
    • Android Support
    • macOS Support
    • Windows Support
  • Unit Tests
  • Sample
  • Documentation

Link to Discussion

#28

Summary

I would like to propose that we add the WithBehavior(this VisualElement visualElement, Behavior behavior) and WithBehaviors(this VisualElement visualElement, params Behavior[] behaviors) extensions that will add the supplied behavior to visualElement.Behaviors.

Motivation

Reduces the amount of code developers need to write ๐Ÿ˜„

Detailed Design

public static class VisualElementExtensions
{
    public static WithBehavior(this VisualElement visualElement, Behavior behavior)
    {
        visualElement.Behaviors.Add(behavior);
        return visualElement;
    }

    public static WithBehaviors(this VisualElement visualElement, params Behavior[] behaviors)
    {
        foreach (var behavior in behaviors)
        {
            WithBehavior(behavior);
        }
        return visualElement;
    }
}

Usage Syntax

Content = new Entry
{
    Placeholder = "Start typing until MaxLength is reached...",
    MaxLength = 100
}.WithBehavior(new MaxLengthReachedBehavior().Bind(
        MaxLengthReachedBehavior.CommandProperty,
        nameof(ViewModel.MaxLengthReachedCommand));

Drawbacks

n/a

Alternatives

n/a

Unresolved Questions

n/a

Is WithBehavior the correct naming approach or should it just be Behavior to match other extensions?

Good point! Looking at the existing extension methods like DynamicResources + Effects, I think we should create one extension method called Behaviors:

public static Behaviors(this VisualElement visualElement, params Behavior[] behaviors)
{
    foreach (var behavior in behaviors)
    {
        visualElement.Behaviors.Add(behavior);
    }
    return visualElement;
}

Hey team & community,

@brminnick @jfversluis @VladislavAntonyuk @pictos

Would you mind voting on whether you would like to approve this PR or not. Given the RC is coming very soon it would be nice to get this in to help with the docs writing.

๐Ÿ‘ for yes and ๐Ÿ‘Ž for no

Reopening Proposal.

Only Proposals moved to the Closed Project Column and Completed Project Column can be closed.

I think this is done with merging the Docs?

I think this is done with merging the Docs?

Oh yeah thanks! Closed now ๐Ÿ˜„