Primary APIs:
ShapePathRectangleEllipseLinePolygonPolylineArcSectorShape.Fill, Shape.Stroke, Shape.StrokeThickness, Shape.StretchGeometry.Parse(...)Geometry.FillContains(...)Geometry.StrokeContains(...)Geometry.GetWidenedGeometry(...)Reference source files:
src/Avalonia.Controls/Shapes/Shape.cssrc/Avalonia.Controls/Shapes/Path.cssrc/Avalonia.Controls/Shapes/Rectangle.cssrc/Avalonia.Controls/Shapes/Ellipse.cssrc/Avalonia.Controls/Shapes/Line.cssrc/Avalonia.Controls/Shapes/Polygon.cssrc/Avalonia.Controls/Shapes/Polyline.cssrc/Avalonia.Controls/Shapes/Arc.cssrc/Avalonia.Controls/Shapes/Sector.cssrc/Avalonia.Base/Media/Geometry.csUse the right primitive:
PathIcon: semantic UI icon element.Shape controls: declarative vector UI and styling.Control.Render(...): specialized immediate-mode drawing.Rule of thumb:
Shape controls for maintainable XAML and style-driven visuals.Common properties from Shape:
FillStrokeStrokeThicknessStrokeDashArrayStrokeDashOffsetStrokeLineCapStrokeJoinStrokeMiterLimitStretchSpecialized APIs:
Path.DataRectangle.RadiusX, Rectangle.RadiusYLine.StartPoint, Line.EndPointPolygon.Points, Polygon.FillRulePolyline.Points, Polyline.FillRuleArc.StartAngle, Arc.SweepAngleSector.StartAngle, Sector.SweepAngleXAML path data:
<Path xmlns="https://github.com/avaloniaui"
Stroke="Orange"
StrokeThickness="2"
Fill="#2200AEEF"
Data="M 10,10 L 90,10 90,40 10,40 Z" />
Shared geometry resources:
<UserControl.Resources>
<StreamGeometry x:Key="BadgeGeometry">M 0,0 L 16,0 16,16 0,16 Z</StreamGeometry>
</UserControl.Resources>
<Path Data="{StaticResource BadgeGeometry}" Fill="LimeGreen" />
Code construction:
using Avalonia.Controls.Shapes;
using Avalonia.Media;
var shape = new Path
{
Data = Geometry.Parse("M 0,0 L 24,0 12,16 Z"),
Fill = Brushes.DodgerBlue,
Stroke = Brushes.Navy,
StrokeThickness = 1
};
For geometry-driven interaction logic:
var geometry = Geometry.Parse("M 0,0 L 24,0 12,16 Z");
var point = new Avalonia.Point(8, 6);
bool insideFill = geometry.FillContains(point);
bool insideStroke = geometry.StrokeContains(new Pen(Brushes.Black, 2), point);
Useful APIs:
Geometry.BoundsGeometry.GetRenderBounds(pen)Geometry.GetWidenedGeometry(pen)StrokeThickness affects effective geometry and often measure behavior.For render-intensive scenarios, evaluate custom drawing (references/14-custom-drawing-text-shapes-and-skia.md).
FillRule intentionally for self-intersecting polygons/polylines.Rectangle/Ellipse over generic Path when possible).Fill and Stroke.StrokeThickness too small for the dash array scale.StartAngle + SweepAngle sign/units are incorrect.