Skip to content

Border StrokeShape should be a Geometry, not a ShapeΒ #3558

Open

Description

@vanka78bg

Description

This is an API design oversight, I believe. The Border component has properties, such as Stroke, StrokeThickness, Fill etc. The border shape is specified via the StrokeShape property of type IShape. The implementing Shape class also has properties, such as Stroke, StrokeThickness, Fill etc. This is really confusing, because it is not known which set of properties is respected.

In my humble opinion, the correct type of the stroke shape should be Geometry, similar to what we use for the Clip property. In fact, it would be nice to clip the inner contents of the Border to that Geometry. For consistency, the property should be renamed to StrokeGeometry as well.

In addition to this, I believe it is not very convenient to require specifying the border shape always, to see any border on the screen. If the border shape is not specified, it should default to a rectangular border, as this is the most common use case.

API Changes

  1. Remove or make obsolete the StrokeShape property of the Border class.
  2. Add a new property StrokeGeometry of type Geometry to the Border class.
  3. Make the StrokeGeometry optional, defaulting to a RectangleGeometry, if not specified.

Usage Scenarios

var border = new Border
{
    Stroke = new SolidColorBrush(Colors.Red),
    StrokeGeometry = new RectangleGeometry(),
    StrokeThickness = 5
};
<Border Stroke="Red" StrokeThickness="5">
    <Border.StrokeGeometry>
        <RectangleGeometry />
    </Border.StrokeGeometry>
</Border>

Backward Compatibility

If the original StrokeShape property is made obsolete, then the backward compatibility can be maintained. I vote for introducing a breaking change by removing the StrokeShape property and replacing it with the new StrokeGeometry property, however. Having a brand new framework with some legacy APIs from day one is not very good in my books.

Difficulty

Medium

Activity

  1. changed the title [-]Border StrokeShape should be Geometry, not a Shape[/-] [+]Border StrokeShape should be a Geometry, not a Shape[/+] on Nov 26, 2021
  2. vanka78bg commented on Nov 26, 2021

    @vanka78bg
    Author

    After investigating the matter some more, I have discovered that both Shape and Geometry classes implement the IShape interface. Setting the StrokeShape property to an instance derived from Shape renders the border correctly, while setting it to an instance derived from Geometry does not render any border at all. Now, I do not know whether this is a bug, or just the API is letting me to assign values of unsupported types. Anyway, I am more convinced this API needs some serious rethinking.

    I would suggest having two interfaces: IGeometry and IShape, as these interfaces are intended to be consumed by the Handlers. The IShape interface can inherit form IGeometry. Now it would be possible to expose all these Stroke, StrokeThickness, Fill etc. properties from the IShape interface. This way the Handlers would not have to cast IShape to Shape every time to get access to them.

  3. added
    t/bug
    on Nov 26, 2021
  4. self-assigned this
    on Nov 26, 2021
  5. vanka78bg commented on Nov 26, 2021

    @vanka78bg
    Author

    Another thing to note: currently it is not possible to set a rectangular shape in XAML. For instance, the following XAML code snippet would not work:

    <Border>
        <Border.StrokeShape>
            <Rectangle />
        </Border.StrokeShape>
    </ Border>

    There is a naming conflict between Microsoft.Maui.Graphics.Rectangle and Microsoft.Maui.Controls.Shapes.Rectangle. Apparently, the XAML parser cannot determine which type to use, so it tries to set the wrong one. This is rather inconvenient, because Rectangle is the most common border type. Using a Geometry would allows us to specify it like this:

    <Border>
        <Border.StrokeShape>
            <RectangleGeometry />
        </Border.StrokeShape>
    </ Border>
  6. added
    area-drawing
    on Nov 29, 2021
  7. shimingsg commented on Mar 18, 2022

    @shimingsg

    verified with VS 17.2.0 Preview 3.0 [32317.292.main], the Rectangle is just in Microsoft.Maui.Controls.Shapes
    the following XAML code snippet works fine but RectangleGeometry was not found in Border

    <Border>
        <Border.StrokeShape>
            <Rectangle />
        </Border.StrokeShape>
    </ Border>
    

    image
    demo: MauiApp3558.zip

  8. added
    s/verified
    on Mar 18, 2022
  9. added this to the .NET 7 milestone on Mar 23, 2022
  10. added
    partner
    on Apr 19, 2022
  11. added
    high
    on May 10, 2022
  12. whichone commented on Jul 3, 2022

    @whichone

    It seems that only Ellipse, Recangle, RoundRectangle can be resized properly based on the content inside. Other shapes like line, path, polyline etc, are drawn with fixed dimensions and won't be resized to hold the content as a BORDER. What's the scenario to use them then?

  13. Ghasan commented on Jul 11, 2022

    @Ghasan

    I don't think it needs to be a Geometry, but I understand the confusion, which needs to be fixed. Maybe into making Rectangle, RectangeShape, and RectangeGeometry?

    It needs to stay as IShape so that we don't need to define size for border shape, we don't really care about the geometry of the border, but rather the outer shape, size is managed by HeightRequest and WidthRequest etc.

    <Border.StrokeShape>
        <RoundRectangle CornerRadius="15,15,0,0" /> <!-- won't be possible with Geometry, as I need to define the rect dimensions then. -->
    </Border.StrokeShape>
  14. 2 remaining items

  15. added
    s/triaged
    s/verified
    on Apr 14, 2023
  16. jinxinjuan commented on Apr 14, 2023

    @jinxinjuan

    Verified with VS 17.6.0 Preview 3.0, The StrokeShape is Geometry, but property StrokeGeometry is not found in Border.
    image

  17. mattleibow commented on Nov 3, 2023

    @mattleibow
    Member

    @PureWeen @drasticactions Related to offline discussions.

  18. mattleibow commented on Nov 3, 2023

    @mattleibow
    Member

    Related to #16919

  19. added this to the Backlog milestone on Nov 3, 2023
  20. added
    t/enhancement β˜€οΈ
    and removed
    t/bug
    partner
    high
    on Nov 3, 2023
  21. dalux-tke commented on Jan 28, 2026

    @dalux-tke

    This issue was linked to here and closed. However, I am still having the issue in the closed ticket as the live preview crashes due to that error.

    Is there any news on the state of this old issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

area-drawingproposal/opens/triageds/verifiedt/enhancement β˜€οΈ

Type

No type

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions