Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Xamarin Xaml Styles for background in ContentPage

Tags:

c#

xaml

xamarin

Newly working in Xamarin and it appears that if I try to set a style for ContentPage nothing happens

Xaml in App.xaml's ResourceDictionary:

<Style TargetType="ContentPage">
    <Setter Property="BackgroundColor" Value="#f8f8f8"/>
</Style>

I know that the app.xaml style is being read. I have a button style that has been applied globally. But I cannot seem to affect any change on ContentPage. No errors are being reported.

How can I globally set the backgroundcolor?

I've read that this might be a bug, but that was a year ago. If it is still a bug is there a workaround?

like image 841
crthompson Avatar asked Jan 26 '26 10:01

crthompson


1 Answers

Following code in App.xaml works for me:

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
   <Setter Property="BackgroundColor" Value="Lime" />
</Style>

Note:

  • You should NOT use attribute x:Key in Style.
  • You should add ApplyToDerivedTypes="True"

I found solution at https://putridparrot.com/blog/styles-in-xamarin-forms/

like image 55
sergtk Avatar answered Jan 27 '26 22:01

sergtk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!