【发布时间】:2023-03-29 08:13:01
【问题描述】:
我在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;
颜色已应用。
【问题讨论】:
-
必答题:设计器中没有设置背景?
-
目前没有可用的设计器。您只能使用 XML 或 C#。
-
改写:BackgroundColor 依赖属性是否在 XML 或 C# 中的任何位置显式设置?
-
BackgroundColor 依赖属性到底是什么意思?我已经在应用程序级别定义了一个样式(使用背景设置器),我想在页面上使用它。在这种情况下,背景颜色在 XML 中设置。
-
可以帮助this主题。
标签: xaml xamarin xamarin.forms