[Proposal] Add methods for all VisualElement Properties
See original GitHub issueAdd methods for all VisualElement Properties
- Proposed
- Prototype
- Implementation: PR raised
- iOS Support
- Android Support
- macOS Support
- Windows Support
- Unit Tests: at least one for each new method following the conventions for other methods in the class
- Sample: Some uses of new methods added to the Markup sample app
- Documentation: PR raised in docs repo
Link to Discussion
https://github.com/CommunityToolkit/Maui.Markup/discussions/140
Summary
As part of ensuring that Maui.Markup can provide a fluent interface for all standard controls, it will be a good starting point for having fluent methods for all properties of VisualElement
as this is the base class to a lot of common controls.
Motivation
Fill in some common gaps in the fluent methods for defining a control instance. Avoid needing to use the clunky method of combining an object initializer and chained fluent methods.
Detailed Design
Fill in the gaps of extension methods for setting the remaining properties of VisualElement
that do not have them.
The properties are:
- AnchorX
- AnchorY
- Background
- BackgroundColor
- Clip
- FlowDirection
- Opacity
- InputTransparent
- IsEnabled
- IsVisible
- RotationX
- RotationY
- Rotation
- ScaleX
- ScaleY
- TranslationX
- TranslationY
- ZIndex
These should all be simple extension methods that wrap setting the property and then returning the element.
The implementations will be of the style:
public static TVisualElement AnchorX<TVisualElement >(this TVisualElement element, double anchorX) where TVisualElement : VisualElement
{
element.AnchorX = anchorX;
return element;
}
In addition to simple methods for the above properties, I also think it would be useful to add the following helper methods:
A method Anchor()
that takes two values that can be applied to AnchorX
and AnchorY
with a single call.
A method Scale()
that takes two values that can be applied to ScaleX
and ScaleY
with a single call.
Usage Syntax
With this change, it will be possible to write
new Label().Text("hello").Opacity(0.5).BackgroundColor(Colors.Red);
rather than having to write
new Label{ Opacity = 0.5, BackgroundColor = Colors.Red }.Text("hello")
Drawbacks
It makes the assembly slightly larger by adding methods where some of these might be rarely used. It feels an appropriate trade-off for completeness though.
Alternatives
No real alternatives other than not doing this. It just makes it easier to provide a more consistent way of writing code when defining a UI in C#.
Unresolved Questions
n/a
Issue Analytics
- State:
- Created 10 months ago
- Comments:6 (4 by maintainers)
Top GitHub Comments
Thanks again, Matt! Just doing some cleanup now, realizing that I forgot to close this as completed 🤦♂️ 😆
Reopening Proposal.
Only Proposals moved to the
Closed
Project Column andCompleted
Project Column can be closed.