Quickstart: Shapes for Windows Phone
[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]
In Windows Phone, there are several options for creating shapes and drawing them on the screen. This Quickstart introduces shapes, such as ellipses, rectangles, polygons, and paths. This Quickstart focuses on creating shapes by using XAML.
If you do a lot of work with shapes, you probably will want to use a WYSIWYG tool like Blend for Visual Studio, which you can use to develop apps for Windows Phone. A WYSIWYG tool simply generates XAML, so understanding the underlying XAML is worthwhile.
This topic contains the following sections.
There are two sets of classes that define a region of space: Shape and Geometry. The main difference between these classes is that a Shape has a brush associated with it and can be rendered to the screen, while a Geometry simply defines a region of space and is not rendered. You can think of a Shape as a UIElement with its boundary defined by a Geometry. This Quickstart just covers Shape classes.
In order for a Shape to be rendered to the screen, a Brush must be associated with it. The Fill property of the Shape is set to the desired Brush. The different types of brushes in Windows Phone are: SolidColorBrush, LinearGradientBrush, RadialGradientBrush, and ImageBrush. For more information about Brushes, see the Quickstart: Brushes for Windows Phone.
Windows Phone enables you to round the corners of a Rectangle. To create rounded corners, specify a value for the RadiusX and RadiusY properties. These properties specify the x and y axes of an ellipse that defines the curve of the corners. The maximum value of RadiusX is the Width divided by two and the maximum value of RadiusY is the Height divided by two.
All Shape classes have Stroke and StrokeThickness properties. Stroke specifies the Brush that's used to paint the border of the Shape. If a Brush for Stroke isn't specified, then the border around the shape isn't drawn. StrokeThickness specifies the width of the border.
The following example creates a Rectangle with a Width of 100 and Height of 50. It uses a SteelBlue SolidColorBrush for its Fill and a Black SolidColorBrush for its Stroke. The StrokeThickness is set to 3 and the RadiusX and RadiusY properties are set to 10, which gives the Rectangle rounded corners. Use the sliders in this example to experiment with different property values.
<Rectangle Fill="SteelBlue" Height="275" Width="350" Stroke="AliceBlue" StrokeThickness="15" RadiusX="40" RadiusY="30"/>
The following image shows a Rectangle with rounded corners displayed on a Windows Phone.
A Polygon is a shape with a boundary defined by an arbitrary number a points. The boundary is created by connecting a line from one point to the next, with the last point connected to the first point. The Points property defines the collection of points that make up the boundary. In XAML, you define the points with a comma-separated list. In code, you use a PointCollection to define the points and you add each individual point as a Points structure to the collection.
The following example creates a Polygon with four points set to (10,200), (60,140), (130,140), and (180,200). It uses a Blue SolidColorBrush for its Fill. No Stroke is defined, so it's possible to arrange the points so that nothing is rendered. If a Stroke is defined, at least a line or a point as thick as the Stroke is drawn. Use the sliders in this example to experiment with the coordinates of the different points. Also, note that if you select the Stroke check box, the last point is connected to the first point.
A Polyline is similar to a Polygon in that the boundary of the shape is defined by a set of points. The main difference between a Polyline and a Polygon is that the last point in a Polyline isn't connected to the first point. The Fill of a Polyline still paints the interior space of the shape, even if the ends of the boundary (or Stroke) don't meet.
As with a Polygon, the Points property defines the collection of points that make up the boundary. In XAML, you define the points with a comma-separated list. In code, you use a PointCollection to define the points and you add each individual point as a Points structure to the collection.
The following example creates a Polyline with four points set to (10,200), (60,140), (130,140), and (180,200). A Fill isn't defined, but a Stroke is defined. Notice that the last and first points aren't connected as they are in a Polygon. Use the sliders in this example to experiment with the coordinates of the different points. Compare the differences between a Polyline and a Polygon when the Fill and Stroke check boxes are changed.
The following example creates a Path that's comprised of a Bezier curve segment and a line segment. The Data attribute string begins with the move command, indicated by M, which establishes a start point for the path. The capital M indicates an absolute location for the new current point. A lowercase m indicates a relative location. The first segment is a cubic Bezier curve that begins at (100,200) and ends at (400,175), which is drawn by using the two control points (100,25) and (400,350). This segment is indicated by the C command in the Data attribute string. Again, the capital C indicates an absolute path; the lowercase c indicates a relative path.
The second segment begins with an absolute horizontal line command H, which specifies a line drawn from the preceding subpath's endpoint (400,175) to a new endpoint (280,175). Because it is a horizontal line command, the value specified is an x-coordinate.
Path data parameters are case-sensitive.
<Path Stroke="DarkGoldenRod" StrokeThickness="3" Data="M 100,200 C 100,25 400,350 400,175 H 280" />
The following image show the Path displayed on a Windows Phone.
On Windows Phone graphics (and some animations) run on a separate thread. For most scenarios, you won't have to worry about this since the system takes care of managing all of this for you. But if your app is going to have a lot of graphics and animations, there are performance and design considerations you'll want to consider.
For more information about performance, see App performance considerations for Windows Phone 8.