{"id":47582,"date":"2020-07-23T09:42:33","date_gmt":"2020-07-23T16:42:33","guid":{"rendered":"https:\/\/devblogs.microsoft.com\/xamarin\/?p=47582"},"modified":"2020-07-23T09:43:24","modified_gmt":"2020-07-23T16:43:24","slug":"xamarin-forms-shapes-and-paths","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/xamarin\/xamarin-forms-shapes-and-paths\/","title":{"rendered":"Drawing UI with Xamarin.Forms Shapes and Paths"},"content":{"rendered":"<p>Xamarin.Forms 4.7 introduces drawing controls for Shapes and Paths. These are powerful new cross-platform controls that use the familiar control syntax from UWP and WPF. As well as use the native graphics libraries on each target platform. <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/graphics\/skiasharp\/\">SkiaSharp<\/a> and <a href=\"https:\/\/github.com\/praeclarum\/NGraphics\">NGraphics<\/a> have long been the &#8220;Go-To&#8221; solutions for rendering drawn UI to support advanced designs. They also continue to be excellent choices based on your individual needs. Now Shapes and Paths bring the power of drawing closer to you by shipping &#8220;in-the-box&#8221;.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-landscape.png\" alt=\"Ellipse, Rectangle, Polygon, and Path\" \/><\/p>\n<h2>Shapes<\/h2>\n<p>There are many built-in controls available in the new API including ellipse, line, polygon, polyline, and rectangle. Each of these shapes support common styling properties. Such as aspect, fill, and a variety of stroke options. Here are two examples where these shapes help quickly implement a design.<\/p>\n<blockquote>\n<p><strong>Enable the Flag<\/strong> &#8211; Shapes are an experimental preview. First <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/internals\/experimental-flags\">add the flag<\/a> to your <code>App.xaml.cs<\/code> at the top of the constructor: <code>Device.SetFlags(new string[]{ \"Shapes_Experimental\" });<\/code>.<\/p>\n<\/blockquote>\n<h3>Custom Step Indicator<\/h3>\n<p>When design requires the use of primitive shapes, using these graphics are a huge time-saver. Consider this custom progress indicator design that shows circles separated by dashes:<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-indicators.png\" alt=\"\" \/><\/p>\n<pre><code class=\"xml\">&lt;StackLayout Orientation=\"Horizontal\" HorizontalOptions=\"Center\" &gt;\n    &lt;StackLayout.Resources&gt;\n        &lt;Style TargetType=\"Ellipse\"&gt;\n            &lt;Setter Property=\"Fill\" Value=\"#33222222\"\/&gt;                    \n            &lt;Setter Property=\"WidthRequest\" Value=\"20\"\/&gt;\n            &lt;Setter Property=\"HeightRequest\" Value=\"20\"\/&gt;\n            &lt;Setter Property=\"VerticalOptions\" Value=\"Center\"\/&gt;\n        &lt;\/Style&gt;\n        &lt;Style TargetType=\"Line\"&gt;\n            &lt;Setter Property=\"Stroke\" Value=\"#33222222\"\/&gt;\n            &lt;Setter Property=\"X2\" Value=\"40\"\/&gt;\n            &lt;Setter Property=\"VerticalOptions\" Value=\"Center\"\/&gt;\n        &lt;\/Style&gt;\n    &lt;\/StackLayout.Resources&gt;\n\n    &lt;Ellipse Fill=\"#FF9900\" \/&gt;\n    &lt;Line \/&gt;\n    &lt;Ellipse \/&gt;\n    &lt;Line \/&gt;\n    &lt;Ellipse \/&gt;\n&lt;\/StackLayout&gt;\n<\/code><\/pre>\n<p>Take this even further and use a <code>VisualStateManager<\/code> to customize the behavior!<\/p>\n<h3>Circle Image<\/h3>\n<p>One of the most powerful uses of shapes is the ability to clip other controls. This is also called &#8220;masking&#8221;. In this example, a square profile image can become a circle by applying an <code>EllipseGeometry<\/code> to the <code>Image.Clip<\/code> property.<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-clipping.png\" alt=\"Circle clipping\" \/><\/p>\n<pre><code class=\"xml\">&lt;Grid ColumnDefinitions=\"*,279,*\" \n          RowDefinitions=\"*,160,350,*\"&gt;\n\n    &lt;!-- Profile Circle Image --&gt;\n    &lt;Ellipse\n        Grid.Row=\"1\"\n        Grid.Column=\"1\"\n        HorizontalOptions=\"Center\"\n        VerticalOptions=\"Center\"\n        WidthRequest=\"168\"\n        HeightRequest=\"168\"\n        Stroke=\"#FFFF9900\"\n        StrokeThickness=\"4\"\n        Fill=\"White\"\/&gt;\n    &lt;Image\n        Grid.Row=\"1\"\n        Grid.Column=\"1\"\n        HorizontalOptions=\"Center\"\n        VerticalOptions=\"Center\"\n        WidthRequest=\"150\"\n        HeightRequest=\"150\"        \n        Source=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2019\/03\/Screen-Shot-2017-01-03-at-3.35.53-PM-150x150.png\"&gt;\n        &lt;Image.Clip&gt;\n            &lt;EllipseGeometry\n                Center=\"75,75\"\n                RadiusX=\"75\"\n                RadiusY=\"75\"\/&gt;\n        &lt;\/Image.Clip&gt;\n    &lt;\/Image&gt;\n\n    &lt;!-- Login Form --&gt;\n&lt;\/Grid&gt;\n<\/code><\/pre>\n<p>The above code does more than just clip the image. It uses another <code>Ellipse<\/code> to create a border effect around the photo by setting a white fill and an orange stroke.<\/p>\n<h2>Paths<\/h2>\n<p>Use Paths for irregular shapes and even custom designs. If you have been using SVG images, then you are already using paths. Open any SVG in a text editor and see a Path node in the XML with a data string to copy directly into your Xamarin.Forms XAML. That is how the shape in the login view pictured above was created.<\/p>\n<pre><code class=\"xml\">&lt;Path\n    HorizontalOptions=\"Fill\"\n    VerticalOptions=\"Fill\"\n    Fill=\"{AppThemeBinding Dark=#333333, Light=#FFFFFF}\"\n    Data=\"M251,0 C266.463973,-2.84068575e-15 279,12.536027 279,28 L279,276 C279,291.463973 266.463973,304 251,304 L214.607,304 L214.629319,304.009394 L202.570739,304.356889 C196.091582,304.5436 190.154631,308.020457 186.821897,313.579883 L186.821897,313.579883 L183.402481,319.283905 C177.100406,337.175023 160.04792,350 140,350 C119.890172,350 102.794306,337.095694 96.5412691,319.115947 L96.5273695,319.126964 L92.8752676,313.28194 C89.5084023,307.893423 83.6708508,304.544546 77.3197008,304.358047 L65.133,304 L28,304 C12.536027,304 1.8937905e-15,291.463973 0,276 L0,28 C-1.8937905e-15,12.536027 12.536027,2.84068575e-15 28,0 L251,0 Z\"\n    \/&gt;\n<\/code><\/pre>\n<blockquote>\n<p>Notice in the above code, the Fill color is set using an AppThemeBinding so it will respond to dark and light themes.<\/p>\n<\/blockquote>\n<h2>Community Ignited<\/h2>\n<p>It has been a pleasure watching the Xamarin.Forms community rapidly generate so many good looking design challenges using the new Shapes and Paths controls. Here are a few that might inspire you:<\/p>\n<table>\n<tr>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-devcrux.gif\" width=\"200\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-devcrux-cinema.png\" width=\"200\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/11803107\/86255618-b46c4a00-bb8d-11ea-9260-f6d09519c5bc.jpg\" width=\"200\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      <a href=\"https:\/\/github.com\/devcrux\/Xamarin.Forms-Path---Watch-Shop\">Source<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/github.com\/devcrux\/Cinema-App-Using-SkiaSharp-and-Xamarin-Fomrs-Shapes\">Source<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/github.com\/Altevir\/xamarinforms-furniture\">Source<\/a>\n    <\/td>\n<\/tr>\n<\/table>\n<table>\n<tr>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/user-images.githubusercontent.com\/11803107\/85948531-c6e54a00-b927-11ea-84ef-378564ca4cfd.jpg\" width=\"200\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/marcofolio.net\/wp-content\/uploads\/2020\/06\/geometric-background-iphone.png\" width=\"200\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/shapes-xamarin-guy.png\" width=\"200\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      <a href=\"https:\/\/github.com\/Altevir\/xamarinforms-delivery\">Source<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/github.com\/marcofolio\/Geometric\">Source<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/www.youtube.com\/watch?time_continue=1&#038;v=IGtTmryOERc&#038;feature=emb_logo\">Video<\/a>\n    <\/td>\n<\/tr>\n<\/table>\n<p>And the list goes on! Here are some others to explore:<\/p>\n<ul>\n<li><a href=\"https:\/\/twitter.com\/XamarinGuy\/status\/1276821768737484801\">https:\/\/twitter.com\/XamarinGuy\/status\/1276821768737484801<\/a><\/li>\n<li><a href=\"https:\/\/twitter.com\/jsuarezruiz\/status\/1274745705362608129\">https:\/\/twitter.com\/jsuarezruiz\/status\/1274745705362608129<\/a><\/li>\n<li><a href=\"https:\/\/twitter.com\/jsuarezruiz\/status\/1279822752703283200\">https:\/\/twitter.com\/jsuarezruiz\/status\/1279822752703283200<\/a><\/li>\n<li><a href=\"https:\/\/www.youtube.com\/watch?v=lpfWJcQ07FI\">https:\/\/www.youtube.com\/watch?v=lpfWJcQ07FI<\/a><\/li>\n<\/ul>\n<p>When working with shapes, it is useful to have good design tools from the start. Javier Suarez Ruiz <a href=\"https:\/\/javiersuarezruiz.wordpress.com\/2020\/06\/30\/xamarin-forms-tips-and-tricks-working-with-shapes\/\">blogged about the software that he uses for this<\/a>.<\/p>\n<h2>In Closing<\/h2>\n<p>This is just the tip of the iceberg for what you can achieve with Shapes and Paths! Learn when to use Shapes and when to use Frame and BoxView by comparing some key differences:<\/p>\n<table>\n<tr>\n<td>\n    <\/td>\n<td>\n      <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/boxview\">BoxView<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/layouts\/frame\">Frame<\/a>\n    <\/td>\n<td>\n      <a href=\"https:\/\/docs.microsoft.com\/xamarin\/xamarin-forms\/user-interface\/shapes\/\">Rectangle<\/a>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Border Color\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Border Width\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Clip\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Contain Content\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Corner Radius\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<\/tr>\n<tr>\n<td>\n      Shadow\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox-checked.png\" \/>\n    <\/td>\n<td>\n      <img decoding=\"async\" src=\"https:\/\/devblogs.microsoft.com\/xamarin\/wp-content\/uploads\/sites\/44\/2020\/07\/checkbox.png\" \/>\n    <\/td>\n<\/tr>\n<\/table>\n<p>What are you creating using Shapes and Paths? Join <a href=\"https:\/\/github.com\/xamarin\/xamarin.forms\/discussions\/shapes\">this discussion on GitHub<\/a> and let us know!<\/p>\n<p>Below are some additional resources in addition to the samples shown above.<\/p>\n<ul>\n<li><a href=\"https:\/\/github.com\/davidortinau\/LoginShape\">LoginShape Sample<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/shapes\/\">Shapes Documentation<\/a><\/li>\n<li><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/shapes\/path\">Paths Documentation<\/a><\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Xamarin.Forms 4.7 introduces drawing controls for Shapes and Paths. These are powerful new cross-platform controls that use the familiar control syntax from UWP and WPF. As well as use the native graphics libraries on each target platform. Learn more!<\/p>\n","protected":false},"author":553,"featured_media":47589,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1,291,367,7745],"tags":[9024,9022,9021,9023,16,5841],"class_list":["post-47582","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xamarin","category-xamarin-platform","category-xamarin-forms","category-xaml","tag-ngraphics","tag-paths","tag-shapes","tag-skiasharp","tag-xamarin-forms","tag-xaml"],"acf":[],"blog_post_summary":"<p>Xamarin.Forms 4.7 introduces drawing controls for Shapes and Paths. These are powerful new cross-platform controls that use the familiar control syntax from UWP and WPF. As well as use the native graphics libraries on each target platform. Learn more!<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47582","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/users\/553"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/comments?post=47582"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/posts\/47582\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media\/47589"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/media?parent=47582"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/categories?post=47582"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/xamarin\/wp-json\/wp\/v2\/tags?post=47582"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}