Xamarin Forms Styling using StyleClass vs Style attribute

madhav.shenoy83madhav.shenoy83 ✭✭AUMember ✭✭

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" />

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" />

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?


  • madhav.shenoy83madhav.shenoy83 ✭✭ AUMember ✭✭

    No one knows???

  • LankyKeLankyKe ✭✭ USMember ✭✭

    "Note the Class attribute for Style (as opposed to the x:Key attribute available in earlier versions of Xamarin.Forms)."
    It seems it's going to be replaced.

  • SteveShaw.5557SteveShaw.5557 ✭✭✭ USMember ✭✭✭
    edited June 2018

    https://stackoverflow.com/a/43985048/199364 (on May 15 '17):

    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.

Sign In or Register to comment.