Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set background color of page with style

I've defined the following explicit style in App.xaml:

<Application.Resources>
    <ResourceDictionary>
      <Style TargetType="ContentPage" x:Key="PageStyle">
        <Setter Property="BackgroundColor" Value="#ff0000" />
      </Style>
    </ResourceDictionary>
</Application.Resources>

The page, which I want to show, is embedded into a NavigationPage and has derived from ContentPage. It has the following implicit style, whereas here ContentPage instead of my derived type is used (in real I use the derived type, but I tried it without and I had the same effect):

<ContentPage.Resources>
    <ResourceDictionary>
      <Style TargetType="ContentPage" BasedOn="{StaticResource PageStyle}" />
    </ResourceDictionary>
</ContentPage.Resources>

But the background of the page doesn't change. It always shows the default background color of the platform. If I use a style for a Button, the style is applied. I tried it with NavigationPage, ContentPage, Page, VisualElement, but the background is always the default background.

If I explicitly set the color with

<ContentPage.BackgroundColor>
    <Color>Red</Color>
</ContentPage.BackgroundColor>

or

this.BackgroundColor = Color.Red;

the color is applied.

like image 605
testing Avatar asked Nov 30 '22 23:11

testing


1 Answers

You can assign it globally, you have to set ApplyToDerivedTypes to true. Here is an example:

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
    <Setter Property="BackgroundColor" Value="#4A4A4A" />
</Style>

Now all classes that inherit ContentPage will have this background color.

like image 162
Trevi Awater Avatar answered Dec 04 '22 20:12

Trevi Awater