Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Forms Styling using StyleClass vs Style attribute

We're building a Xamarin Forms app we've noticed we could style an element in 2 ways by creating styles in the App.xaml ResourceDictionary

Class and StyleClass option

In App.xaml we'll write

        <Style Class="EntryStandard" TargetType="Entry">
            <Setter Property="TextColor" Value="#575e62" />
            <Setter Property="BackgroundColor" Value="#9facb3" />
            <Setter Property="FontSize" Value="14" />
        </Style>

Then this gets used in one of the contentpages like this

<Entry StyleClass="EntryStandard" Placeholder="Login Name" Text="{Binding EntryEmailAddress}" />

Key and Style option

This is what we write under App.xaml

      <Style x:Key="ButtonMainMenu_Purple" TargetType="Button">
            <Setter Property="BackgroundColor" Value="#5d4785" />
            <Setter Property="FontSize" Value="14" />
            <Setter Property="TextColor" Value="#FFFFFF" />
        </Style>

And then we use the following in our contentpages

<Button Style="{StaticResource ButtonMainMenu_Purple}" Text="Friends" Command="{Binding OnFriendsButtonCommand}" />

Both work fine, I just wanted to know which one is better than the other and why?

like image 800
Madhav Shenoy Avatar asked Dec 09 '16 20:12

Madhav Shenoy


People also ask

How to apply styles in Xamarin forms?

Styling a Xamarin. Forms app is traditionally accomplished by using the Style class to group a collection of property values into one object that can then be applied to multiple visual element instances. This helps to reduce repetitive markup, and allows an apps appearance to be more easily changed.

What is dynamic resources in Xamarin forms?

The DynamicResource markup extension is similar to the StaticResource markup extension in that both use a dictionary key to fetch a value from a ResourceDictionary . However, while the StaticResource performs a single dictionary lookup, the DynamicResource maintains a link to the dictionary key.

Can I use bootstrap with Xamarin forms?

We love everything that makes the process of building beautiful applications easier. Using font Icons in Xamarin Forms apps makes this easier. Having to skip the process of creating and adding manually every icon for our application is just awesome.


1 Answers

Regular styles follow the standard, relatively inflexible WPF model. Style classes includes cascade semantics and are part of the new theme support. They are poorly documented, however, and still in beta.

like image 65
Edward Brey Avatar answered Oct 15 '22 08:10

Edward Brey