设置页面的背景颜色与样式

tes*_*ing 3 xaml xamarin xamarin.forms

我在下面定义了以下显式样式App.xaml:

<Application.Resources>
    <ResourceDictionary>
      <Style TargetType="ContentPage" x:Key="PageStyle">
        <Setter Property="BackgroundColor" Value="#ff0000" />
      </Style>
    </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

我想要显示的页面嵌入到a中NavigationPage并从中派生出来ContentPage.它有以下隐式样式,而这里使用的ContentPage不是我的派生类型(实际上我使用派生类型,但我尝试了它没有,我有相同的效果):

<ContentPage.Resources>
    <ResourceDictionary>
      <Style TargetType="ContentPage" BasedOn="{StaticResource PageStyle}" />
    </ResourceDictionary>
</ContentPage.Resources>
Run Code Online (Sandbox Code Playgroud)

但页面的背景不会改变.它始终显示平台的默认背景颜色.如果我使用a的样式Button,则应用样式.我试了一下NavigationPage,ContentPage,Page,VisualElement,但背景始终是默认背景.

如果我明确设置颜色

<ContentPage.BackgroundColor>
    <Color>Red</Color>
</ContentPage.BackgroundColor>
Run Code Online (Sandbox Code Playgroud)

要么

this.BackgroundColor = Color.Red;
Run Code Online (Sandbox Code Playgroud)

应用颜色.

Tre*_*ter 13

您可以全局分配它,您必须设置ApplyToDerivedTypes为true.这是一个例子:

<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
    <Setter Property="BackgroundColor" Value="#4A4A4A" />
</Style>
Run Code Online (Sandbox Code Playgroud)

现在,所有继承ContentPage的类都将具有此背景颜色.


Mic*_*ruk 2

将样式应用到您的 ContentPage,如下所示:

<ContentPage (...) Style="{StaticResource PageStyle}">
Run Code Online (Sandbox Code Playgroud)