{"id":6285,"date":"2018-06-16T20:30:16","date_gmt":"2018-06-16T20:30:16","guid":{"rendered":"http:\/\/putridparrot.com\/blog\/?p=6285"},"modified":"2018-06-16T20:30:16","modified_gmt":"2018-06-16T20:30:16","slug":"xamarin-forms-3-0-supports-css","status":"publish","type":"post","link":"https:\/\/putridparrot.com\/blog\/xamarin-forms-3-0-supports-css\/","title":{"rendered":"Xamarin Forms (>= 3.0) supports CSS"},"content":{"rendered":"<p>In my previous post <a href=\"http:\/\/putridparrot.com\/blog\/styles-in-xamarin-forms\/\" rel=\"noopener\" target=\"_blank\">Styles in Xamarin Forms<\/a> I demonstrated creating a simple styles using XAML Style objects. However, as of version 3.0 of Xamarin Forms, you can also use CSS (Cascading Style Sheet) files\/syntax to define your styles.<\/p>\n<p>XAML Style is quite a verbose way of defining styles but if you&#8217;re used to WPF or UWP it&#8217;s probably something you&#8217;re quite happy with. If, however, you come from a web background you&#8217;ll probably be used to CSS files instead. Now you can use similar syntax to define your styles for Xamarin Forms (3.0 or above).<\/p>\n<p>If you create a standard mobile application in Visual Studio you&#8217;ll get a simple ContentPage based application &#8211; as per the previous post we&#8217;re going to start off by simply defining a Lime green background for our ContentPage&#8217;s.<\/p>\n<p>Now simply add a CSS file (mine&#8217;s named Styles.css) to your solution and set it&#8217;s <strong>Build Action<\/strong> to <strong>Embedded resource<\/strong> and place the following into the file<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n^ContentPage {\r\n     background-color: Lime;\r\n}\r\n<\/pre>\n<p><em>Note: the CSS is case insensitive, but I&#8217;ve stuck to using the case associated with the controls\/page here.<\/em><\/p>\n<p>Notice the use of the ^ followed by the base type. If you read my previous post <a href=\"http:\/\/putridparrot.com\/blog\/styles-in-xamarin-forms\/\" rel=\"noopener\" target=\"_blank\">Styles in Xamarin Forms<\/a>, you&#8217;ll notice that this is equivalent to an implicit style. i.e. it&#8217;s applied to anything of the type ContentPage.<\/p>\n<p>To use this style we need to add the following to our ContentPage XAML<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\n&lt;ContentPage.Resources&gt;\r\n   &lt;StyleSheet Source=&quot;Styles.css&quot; \/&gt;\r\n&lt;\/ContentPage.Resources&gt;\r\n<\/pre>\n<p><em>Note: you do NOT need to add any namespace to use the StyleSheet (or at least I didn&#8217;t at the time of writing). if Visual Studio or Resharper suggests you need to add a namespace, just ignore it.<\/em><\/p>\n<p>Now if you run your application you&#8217;ll be presented with the Lime background ContentPage.<\/p>\n<p>If you&#8217;re used to CSS you&#8217;ll know that you can also declare an id (the equivalent of an explicit style) to your CSS. Let&#8217;s change our CSS to this following <\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n#ContentStyle {\r\n     background-color: Lime;\r\n }\r\n<\/pre>\n<p>Now we can assign this to either the StyleId or it will fallback to using x:Name (if no StyleId is set) on our control. For example <\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nStyleId=&quot;ContentStyle&quot;\r\n<\/pre>\n<p>or if you prefer you can simply use the x:Name (remember no StyleId should exist)<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nx:Name=&quot;ContentStyle&quot;\r\n<\/pre>\n<p>We can also declare our CSS using CLASS selectors such as<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\n.ContentStyle {\r\n     background-color: Lime;\r\n}\r\n<\/pre>\n<p>Now we would use the StyleClass attribute on our ContentPage instead of StyleId or x:Name, i.e.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\r\nStyleClass=&quot;ContentStyle&quot;\r\n<\/pre>\n<p>Finally, we can also declare CSS for child elements, so for example CSS for the Label which is a child of a StackLayout would look like this<\/p>\n<pre class=\"brush: css; title: ; notranslate\" title=\"\">\r\nStackLayout Label {\r\n    background-color: Lime;\r\n}\r\n<\/pre>\n<p>See <a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/styles\/css\/#selector-reference\" rel=\"noopener\" target=\"_blank\">Selector reference<\/a> for more on selector syntax, which also demonstrated setting style for direct child objects etc.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.microsoft.com\/en-us\/xamarin\/xamarin-forms\/user-interface\/styles\/css\/\" rel=\"noopener\" target=\"_blank\">Styling Xamarin.Forms apps using Cascading Style Sheets (CSS)<\/a><br \/>\n<a href=\"https:\/\/visualstudiomagazine.com\/articles\/2018\/04\/01\/styling-xamarin-forms.aspx\" rel=\"noopener\" target=\"_blank\">Styling Xamarin.Forms Apps with CSS<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In my previous post Styles in Xamarin Forms I demonstrated creating a simple styles using XAML Style objects. However, as of version 3.0 of Xamarin Forms, you can also use CSS (Cascading Style Sheet) files\/syntax to define your styles. XAML Style is quite a verbose way of defining styles but if you&#8217;re used to WPF [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[122],"tags":[],"class_list":["post-6285","post","type-post","status-publish","format-standard","hentry","category-xamarin-forms"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6285","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/comments?post=6285"}],"version-history":[{"count":3,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6285\/revisions"}],"predecessor-version":[{"id":6291,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/posts\/6285\/revisions\/6291"}],"wp:attachment":[{"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/media?parent=6285"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/categories?post=6285"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/putridparrot.com\/blog\/wp-json\/wp\/v2\/tags?post=6285"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}