question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Proposal] Add methods for all VisualElement Properties

See original GitHub issue

Add 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:closed
  • Created 10 months ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
brminnickcommented, Feb 2, 2023

Thanks again, Matt! Just doing some cleanup now, realizing that I forgot to close this as completed 🤦‍♂️ 😆

0reactions
msftbot[bot]commented, Dec 6, 2022

Reopening Proposal.

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

Read more comments on GitHub >

github_iconTop Results From Across the Web

Scripting API: VisualElement
VisualElement contains several features that are common to all controls in UIElements, such as layout, styling and event handling.
Read more >
How to properly add elements to custom VisualElement ...
I have a custom VisualElement. And two issues when using it in UIBuilder (a) Every time I edit the element, things are instantiated...
Read more >
UIElements Tutorial for Unity: Getting Started
Each editor window has a rootVisualElement property, which represents the top element of the hierarchy. The root has to add a VisualElement ......
Read more >
Entry - .NET MAUI
Entry defines the following properties: CharacterSpacing , of type double , sets the spacing between characters in the entered text.
Read more >
Sustainable Development in Creative Industries: Embracing ...
2 RESEARCH METHODS The method uses in this research is qualitative. ... definitions, characteristics, metaphors, symbols, and descriptions of things.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found