我在 App.xaml
中定义了以下显式样式:
<Application.Resources>
<ResourceDictionary>
<Style TargetType="ContentPage" x:Key="PageStyle">
<Setter Property="BackgroundColor" Value="#ff0000" />
</Style>
</ResourceDictionary>
</Application.Resources>
我想展示的页面被嵌入到一个NavigationPage
中,并且派生自ContentPage
。它具有以下隐式样式,这里使用ContentPage
而不是我的派生类型(实际上我使用了派生类型,但我尝试了没有使用也得到了相同的效果):
<ContentPage.Resources>
<ResourceDictionary>
<Style TargetType="ContentPage" BasedOn="{StaticResource PageStyle}" />
</ResourceDictionary>
</ContentPage.Resources>
但页面的背景并未改变,它仍然显示平台的默认背景颜色。如果我为“Button”使用样式,那么样式会被应用。我尝试使用“NavigationPage”,“ContentPage”,“Page”,“VisualElement”,但背景始终是默认的。如果我明确地设置颜色:
<ContentPage.BackgroundColor>
<Color>Red</Color>
</ContentPage.BackgroundColor>
或者
this.BackgroundColor = Color.Red;
颜色已应用。