Monday 25 July 2016

Styles in Xamarin


When we're talking about some kind of design in xamarin then it's very easy if you're well 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 appearance of your controls.
In xamarin we have different types of styles. If you're a web developer and a 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 so what you're going to do is??? Probably you would create different textboxes and buttons and assign style properties in each control. What if someone ask you to add hundred buttons in your page with same properties of all buttons. May be you would hardly code all the styles in each button tag. This thing will also work but the purpose of reusability diminish and your code size grows. So the question is what we're 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 is simply add a style reference to call all the properties that you've already defined in your custom styles. 

<ContentPage.Resources>
    <ResourceDictionary>
        <Style x:Key="LabelStyle" TargetType="Label">
            <Setter Property="TextColor" Value="Red"/>
            <Setter Property="FontSize" Value="30"/>
        </Style>
    </ResourceDictionary>
</ContentPage.Resources>  




<StackLayout>
        <Label Text="Check out my style." Style="{StaticResource LabelStyle}" />
    </StackLayout>   



Boom...! We've done that tricky thing.
Keep staying with us for different and tricky tips and articles...

No comments:

Post a Comment