I'm curious is there more convenient way to use themes in Xamarin forms. Known way is add Colors into ResourceDictionary in App.xaml:
<Color x:Key="BackgroundColorDark">#111111</Color>
<Color x:Key="BackgroundColorLight">#ffffff</Color>
and then specify it on element like this:
<Button BackgroundColor="{AppThemeBinding Light={StaticResource DarkPageBackgroundColor},
Dark={StaticResource LightPageBackgroundColor}}" ... >
So my question is there possibility to do it in different way? I have reaaly lots of buttons for example, and I don't want to specify everywhere for each of those buttons that it has to use this color for light theme and that for dark. I want to specify it like: Hey Button, your background color is "ButtonBackgroundColor" and its value depends on current theme, which itself is set in two different ResourceDictionaries, like this:
<ResourceDictionary x:Name="Dark">
<Color x:Key="ButtonBackgroundColor">#000000</Color>
</ResourceDictionary>
<ResourceDictionary x:Name="White">
<Color x:Key="ButtonBackgroundColor">#ffffff</Color>
</ResourceDictionary>
CodePudding user response:
You can use a ButtonStyle for different Buttons
<Color x:Key="LightPrimaryColor">WhiteSmoke</Color>
<Color x:Key="LightSecondaryColor">Black</Color>
<!-- Dark colors -->
<Color x:Key="DarkPrimaryColor">Teal</Color>
<Color x:Key="DarkSecondaryColor">White</Color>
<Style x:Key="ButtonStyle"
TargetType="Button">
<Setter Property="BackgroundColor"
Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
<Setter Property="TextColor"
Value="{AppThemeBinding Light={StaticResource LightSecondaryColor}, Dark={StaticResource DarkSecondaryColor}}" />
</Style>
And in Xaml for a Button add Style
<Button Text="MORE INFO"
Style="{StaticResource ButtonStyle}" />
Info here https://docs.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/theming/system-theme-changes
I use ButtonStyle and made one ButtonStyleA for other Buttons that i want to show in an other way
CodePudding user response:
The answer from Bas H is correct. You should use styles in most scenarios. You can also even use style implicitly, meaning they will be applied by default on all controls of the target type.
<Style TargetType="Button">
<Setter Property="BackgroundColor"
Value="{AppThemeBinding Light={StaticResource LightPrimaryColor}, Dark={StaticResource DarkPrimaryColor}}" />
<Setter Property="TextColor"
Value="{AppThemeBinding Light={StaticResource LightSecondaryColor}, Dark={StaticResource DarkSecondaryColor}}" />
</Style>
// The backgroundcolor and textcolor are applied by default now.
<Button Text="MORE INFO"/>
CodePudding user response:
Agree with comments above, think that most people miss that you can bind to static colours within the style inside the resource dictionary. Very useful when you want to change theme colours, one up is apples to all styles.
I do this but I also allow users to choose colours which i save in a database column, onl oad of the app I'll set the value in the resource dictionary in code ;)
