8
votes

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?

1
:( looks like everyone's on Christmas break.... - Madhav Shenoy
The "Key and Style option" does not work for me. I get: "StaticResource not found for key Banner'". I assume the Style is defined in Application.Resources in the App.xaml file? The "Class and StyleClass option" works fine. - Rob L
Its an old post. Forms has a changed a lot since then. - Madhav Shenoy

1 Answers

5
votes

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.