Introduction:
You may heard that Xamarin.Forms 3.0 introduces the ability to style an app using Cascading Style Sheets (CSS). We can create a style sheet that consists of a list of rules, with each rule consisting of one or more selectors and a declaration block. A declaration block consists of a list of declarations in braces, with each declaration consisting of a property, a colon, and a value. When there are multiple declarations in a block, a semi-colon is inserted as a separator.
Requirements:
- This article source code is prepared by using Visual Studio Community for Mac (7.4). And it is better to install latest visual studio updates from here.
- This sample project is Xamarin.Forms PCL project and tested in Android emulator and iOS simulators.
- Used Xamarin.Forms version is 3.0.0 and we can also use later version.
Description:
This article can explain you below topics:
1. How to create Xamarin.Forms PCL project with Visual Studio for Mac?
2. How to create CSS styles in Xamarin.Forms?
3. How to use CSS styles in Xamarin.Forms?
3. How to use CSS styles in Xamarin.Forms?
1. How to create Xamarin.Forms PCL project with Visual studio for Mac?
First we need to create the new Xamarin.Forms project.
- Launch Visual Studio for Mac.
- On the File menu, select New Solution.
- The New Project dialog appears. In the left pane Multiplatform > App > Xamarin.Forms > Blank Forms App and click on Next.
- Enter your App Name (Ex: FormsXamlCSS). Select Target Platforms to Android & iOS and Shared Code to Portable Class Library after that click on Next button.
- You can choose your project location like below and Create new project.
And project structure will be.
- FormsXamlCSS: It is for PCL shared code.
- FormsXamlCSS.Droid: It is for Android.
- FormsXamlCSS.iOS: It is for iOS.
2. How to create CSS styles in Xamarin.Forms?
Right click on your PCL project => Add => New Folder and name it "Assets". After that we need to create styles file by right click on "Assets" folder => Web => Empty CSS File and name it styles.css like below
Note: Make sure your CSS file should have a Build Action to EmbeddedResource else it will fail to load. After that add below styles to CSS.
styles.css:
- ^contentpage {
- background-color: white
- }
- stacklayout {
- margin: 20;
- background-color: lightgray;
- }
- .detailPageTitle {
- font-style: bold;
- font-size: 35;
- margin-left: 10;
- }
- .detailPageSubtitle {
- text-align: center;
- font-style: italic;
- font-size: medium;
- }
In above file, we added different rules for selectors with different properties.
- Set the background color to white for ContentPage.
- Set the margin value to 20 and background color to LightGray for StackLayout.
- Page Title should have a bolded text with font size 35 and margin left to 10 px.
- Page SubTitle should be center align with italic format and medium font size.
3. How to use CSS styles in Xamarin.Forms?
Now it is time to use our css styles file in the xaml pages. And we need to follow steps below:
Step 1:
We need to use StyleSheet class which will help to load and parse a style sheet. So we can refer our styles sheet like below
Xaml:
- <ContentPage ...>
- <ContentPage.Resources>
- <StyleSheet Source="Assets/styles.css" />
- </ContentPage.Resources>
- ...
- </ContentPage>
Alternatively, the style sheet can be loaded and parsed with the StyleSheet class by inlining it in a CDATA section:
- <ContentPage ...>
- <ContentPage.Resources>
- <StyleSheet>
- <![CDATA[
- ^contentpage {
- background-color: lightgray;
- }
- ]]>
- </StyleSheet>
- </ContentPage.Resources>
- ...
- </ContentPage>
C#
In C#, a style sheet can be loaded as an embedded resource and added to the ResourceDictionary for the page:
- using System.Reflection;
- using Xamarin.Forms;
- using Xamarin.Forms.StyleSheets;
- namespace FormsXamlCSS
- {
- public partial class MainPage : ContentPage
- {
- public MainPage()
- {
- InitializeComponent();
- this.Resources.Add(StyleSheet.FromAssemblyResource(
- IntrospectionExtensions.GetTypeInfo(typeof(MainPage)).Assembly,
- "FormsXamlCSS.Assets.styles.css"));
- }
- }
- }
Step 2:
Now we can apply the styles properties for your controls by referring CSS id's like below:
- <?xml version="1.0" encoding="utf-8" ?>
- <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
- xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
- x:Class="FormsXamlCSS.MainPage"
- Title="Monkey Details">
- <ContentPage.Resources>
- <StyleSheet Source="Assets/styles.css" />
- </ContentPage.Resources>
- <StackLayout>
- <Label Text="Page Title" StyleClass="detailPageTitle" />
- <Label Text="Page SubTitle" StyleClass="detailPageSubtitle"/>
- </StackLayout>
- </ContentPage>
Note:
1. CSS uses selectors to determine which elements to target. Styles with matching selectors are applied consecutively, in definition order. Styles defined on a specific item are always applied last. So in our styles file (styles.css), we taken two selectors are ContentPage and StackLayout.
2. CSS uses properties to style a selected element. Each property has a set of possible values, and some properties can affect any type of element, while others apply to groups of elements.
Also make sure to your all three projects (PCL, Android and iOS) should have Xamarin.Forms Nuget package version should be 3.0 or later. Otherwise you will get xaml parsing exception for StyeleSheet like below:
To check your project current Xamarin.Forms version, right click on your project "Packages" folder.
Currently in above screen, current PCL project having X.F version 2.5.0. We need to Update it for version 3.0 or later like below:
Demo screens from iOS:
You can also directly work on below sample source code to understand this article.
Reference:
https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/styles/
FeedBack Note: Please share your thoughts, what you think about this post, Is this post really helpful for you? I always welcome if you drop comments on this post and it would be impressive.
Error Resource 'Assets/styles.css' not found. (CSSXamarin)
ReplyDeleteI am getting this error. My xamarin.forms version is 3.0 above Still i am facing this problem. please suggest
Test with the path “/Assets/styles.css, or also make sure your styles.css Build Action to EmbeddedResource
Delete