最近在Xamarin工作,如果我尝试为ContentPage
设置样式,那么似乎没有任何事情发生
App.xaml的ResourceDictionary中的Xaml:
<Style TargetType="ContentPage">
<Setter Property="BackgroundColor" Value="#f8f8f8"/>
</Style>
我知道正在阅读app.xaml样式。我有一个全局应用的按钮样式。但我似乎无法影响ContentPage
的任何变化。没有报道任何错误。
如何全局设置backgroundcolor?
我读过这可能是一个错误,但那是一年前的事。如果它仍然是一个错误,是否有解决方法?
答案 0 :(得分:2)
以下App.xaml
中的代码对我有用:
<Style TargetType="ContentPage" ApplyToDerivedTypes="True">
<Setter Property="BackgroundColor" Value="Lime" />
</Style>
注意:
x:Key
中使用属性Style
。ApplyToDerivedTypes="True"
我在https://putridparrot.com/blog/styles-in-xamarin-forms/找到了解决方法
答案 1 :(得分:1)
按照@Tomasz Kowalczyk的提示,使用示例xaml from this rather incomplete post:
在app.xaml中,ResourceDictionary提出了这种风格:
<Style x:Key="defaultPageStyle" TargetType="ContentPage">
<Setter Property="BackgroundColor" Value="#f8f8f8"/>
</Style>
基类名为BaseContentPage
public class BaseContentPage : ContentPage
{
public BaseContentPage()
{
var style = (Style)Application.Current.Resources["defaultPageStyle"];
Style = style;
}
}
然后在每个xaml.cs类中将它们组合在一起:
namespace MyNamespace
{
public partial class MyXamlPage: BaseContentPage
和.xaml文件
<local:BaseContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:local="clr-namespace:MyNamespace;assembly=MyNamespace"
x:Class="MyNamespace.MyXamlPage"