In this blog, we will be going to discuss about how to draw UI using Shapes and Paths in Xamarin.Forms. Xamarin is the best technology for cross-platform mobile applications. Now Xamarin is upgrading and the latest version is Xamarin.Forms 5. Shapes and paths are the latest control for the version of Xamarin.Forms. These controls are more useful for User Interface. Shapes and paths are useful in all platforms like Android, IOS, UWP, WPF, etc. Because shapes and path controls are using the native library. There are many controls in the new version like Shapes, paths, brushes, CarouselView, Drag and Drop, Swipe View, Radio button, etc. These controls to be useful and create a design more beautiful.
There are several built-in controls in xamarin technology. Including a new API with an ellipse, line, polygon, rectangle, polyline, etc. All shapes are supported by common styling properties. You can easily implement such beautiful shapes in an application using the methods below.
- Enable the flag.
- You have to implement custom settings, in the custom step indicator.
The shape is a part of the drawing UI feature of xamarin.forms. The developer can convert a shape into a linear code.
The shape is a type of View that enables to draw the screen. The shape can be used inside layout classes and many controls because the shape class is derived from the view class.
Properties of shapes
Aspect is a type of stretch. It describes how the shape fills its allocated space and by default the Aspects property value is Stretch.None.
Fill is a type of Brush. The brush is used to paint in shape interior.
A stroke is a type of Brush. It can be used to paint the shape’s outline.
StrokeDashArray is a kind of double collection, it represents a collection of double values showing the line and space patterns used to constrain the shape.
StrokeDashOffset is a type of double. This property is used for showing distance between one dash line ending to second-line starting and StrokeDashOffset default value is 0.0.
StrokeLineCap is a type of PenLineCap. This property is used for showing the shape of the line segment. StrokeLineCap property’s default value is PenLineCap.Flat.
StrokeLineJoin is a type of PenLineJoin. This property can define the specific type of join that is used for vertices of the shape. StrokeLineJoin property’s default value is PenLineJoin.Meter.
StrokeMiterLimit is a type of double. This property specifies the limit on the ratio of the miter length for the StrokeThickness of shape.StrokeMiterLimit default value is 10.0.
StrokeThickness is a type of double. This property indicates the width of the shape outline and StrokeThickness default value is 0.0.
These properties are registered by the BindableProperty objects, which means they can be stylized and data-binding targets.
<xmp> <Rectangle Fill="Blue" Stroke="Yellow” StrokeThickness="6" StrokeDashArray="0,1" StrokeDashOffset="8" WidthRequest="130" HeightRequest="90" HorizontalOptions="StartAndExapand" /> </xmp>
<xmp> <Line X1="0" Y1="20" X2="300" Y2="20" StrokeLineCap="Round" Stroke="Red" StrokeThickness="12" /> </xmp>
Paths control is a control of xamarin.forms 5. The path control can be used for the Custom design. If you use an SVG image, then you are already using Paths.
The Path class is derived from the shape class and you have also created complex shapes and curves. These curves and shapes are described using Geometry objects.
Properties of paths
Data is a type of Geometry. This property is use for drawing the particular shapes.
RenderTransform is a type of transform. This property represents the transform that is applied in geometry using a path before it is drawn
These properties are registered by the BindableProperty objects, which means they can be stylized and be the goal of data-bindings.
How to Create a Path?
There are two methods for creating the path the following:
- Create a Path object and set the property using Data. You can set the Data in property in the Geometry object. Geometry object or GeometryGroup acts that can combine multiple geometry objects into a single object.
- You can set Data value in a string in XAML, using path mark-up syntax. This approach to be use and path. Data value is consuming a serialization format for graphics, Typically, a string data value that cannot be edit after created the path.
Create a path With Mark-up Syntax:
Example for Rectangle:
<xmp> <Path Data="M 20,200 L 200 200,200 200,50Z" Stroke="Red" StrokeThickness="3" Aspect="Uniform" HorizontalOptions="Center" /> </xmp>
M -M is the Move Command, Establish the start point for the path.
L -L is a Line Command, which specify the starting and ending point of the line.
Z- Z is the Close Command, which creates a line that connects the starting point.
Create a path with Geometry Objects
Curves and shapes can be defined using geometry objects and it is used to set the data property of the Path object. There are three varieties of objects below.
All geometry is to be used for simple shapes. But if you create complex shapes and curves that can be used as a PathGeometry. PathGeometry objects consist of one or more PathFigure objects. Each PathFigure object represents a different shape. Each PathFigure object itself consists of one or more PathSegment objects, each of which represents the binding part of the shape. There are three segment objects below.
Example using Triangle:
<xmp> <Path Stroke="Black" StrokeThickness="1" Aspect="Uniform" HorizontalOptions="Center"> <Path.Data> <PathGeometry> <PathGeometry.Figures> <PathFigureCollection> <PathFigure IsClosed="True" StartPoint="20,200"> <PathFigure.Segments> <PathSegmentCollection> <LineSegment Point="200,200" /> <LineSegment Point="200,60" /> </PathSegmentCollection> </PathFigure.Segments> </PathFigure> </PathFigureCollection> </PathGeometry.Figures> </PathGeometry> </Path.Data> </Path> </xmp>
The starting point of the triangle is (20,200). A line segment is drawn between (20,200) to (200,200) and (200,200) to (200,60). The numbers in the first and last segment are then linked because of the PathFigure.IsClosed property is set to true.
In this blog, we explain the new features of shapes and paths in Xamarin.Forms 5. Shapes and Paths both controls can be useful for the interactive user interface. These controls are useful in any platform like Android, IOS, UWP, WPF. Paths control can be useful for creating a complex design using geometry in the Xamarin Forms.