Styles in Xamarin

When we're talking about some kind of design in Xamarin then it's very easy if you're familiar with the styles of Xamarin.

So the question is, what is style?

A style is a property of your control that tells your control how to look in different scenarios. Styles can be used to create the appearance of your controls.

In Xamarin we have different types of styles. If you're a web developer and have know how of CSS (Cascading style sheets) then it’s very easy for you to learn style in Xamarin and xaml.

So let's dive into the depth of styles.

In Xamarin we're going to cover lot of design aspects. So let’s get started.

When someone tells you to create a form or application first page which includes different text boxes and different buttons what are you going to do? Probably you would create different text boxes and buttons and assign style properties in each control. What if someone asks you to add a hundred buttons in your page with the same properties for all buttons? Maybe you would hardly code all the styles in each button tag. This will also work, but the purpose of reusability diminishes and your code size grows. So the question is, what are we going to do?

So here is the solution of this.

We simply add a following code and now write the Label controls simply without setting any property. What you'll need to do  is simply add a style reference to call all the properties that you've already defined in your custom styles.

  1. <ResourceDictionary>  
  2.     <Style x:Key="LabelStyle" TargetType="Label">  
  3.         <Setter Property="TextColor" Value="Red"/>   
  4.         <Setter Property="FontSize" Value="30"/>  
  5.     </Style>  
  6. </ResourceDictionary>  
  7. </ContentPage.Resources>  
  1. <StackLayout>  
  2.    <Label Text="Check out my style." Style="{StaticResourceLabelStyle}" />  
  3. </StackLayout>  
Boom! We've done that tricky thing.

Keep staying with us for different and tricky tips and articles.

 

Up Next
    Ebook Download
    View all
    Learn
    View all