Xamarin Forms - 无法使用 TextColor 覆盖样式 属性
Xamarin Forms - Can't Override Style With TextColor Property
我的标签样式定义如下:
var myLabelStyle = new Style(typeof(Label))
{
Setters =
{
new Setter { Property = Label.TextColorProperty, Value = Color.Blue },
new Setter { Property = Label.FontSizeProperty, Value = 30 }
}
};
然后我有一个这样定义的标签:
var myLabel = new Label
{
Text = "My Label",
Style = myLabelStyle,
TextColor = Color.Red
};
那个标签的颜色不应该是红色吗?好吧,它是蓝色的。
根据直觉和 Xamarin docs(请参阅屏幕截图后的单个句子),我应该能够覆盖样式中定义的任何给定 属性。
什么给了?
我没有发现您的代码有任何问题:,它在这个例子中有效:
var myLabelStyle = new Style(typeof(Label))
{
Setters =
{
new Setter { Property = Label.TextColorProperty, Value = Color.Blue },
new Setter { Property = Label.FontSizeProperty, Value = 30 }
}
};
var myLabelRed = new Label
{
Text = "My Red Label",
Style = myLabelStyle,
TextColor = Color.Red
};
var myLabelBlue = new Label
{
Text = "My Blue Label",
Style = myLabelStyle,
};
var content = new ContentPage
{
Content = new StackLayout
{
Children = {
myLabelRed,
myLabelBlue
}
}
};
iOS:
Android:
它似乎是一个 Xamarin Forms 错误 在版本 2.0.0.6482(按预期工作)和 版本 2.2.0.31 之间的某个地方引入(坏了!没有按预期工作)。
我的标签样式定义如下:
var myLabelStyle = new Style(typeof(Label))
{
Setters =
{
new Setter { Property = Label.TextColorProperty, Value = Color.Blue },
new Setter { Property = Label.FontSizeProperty, Value = 30 }
}
};
然后我有一个这样定义的标签:
var myLabel = new Label
{
Text = "My Label",
Style = myLabelStyle,
TextColor = Color.Red
};
那个标签的颜色不应该是红色吗?好吧,它是蓝色的。
根据直觉和 Xamarin docs(请参阅屏幕截图后的单个句子),我应该能够覆盖样式中定义的任何给定 属性。
什么给了?
我没有发现您的代码有任何问题:,它在这个例子中有效:
var myLabelStyle = new Style(typeof(Label))
{
Setters =
{
new Setter { Property = Label.TextColorProperty, Value = Color.Blue },
new Setter { Property = Label.FontSizeProperty, Value = 30 }
}
};
var myLabelRed = new Label
{
Text = "My Red Label",
Style = myLabelStyle,
TextColor = Color.Red
};
var myLabelBlue = new Label
{
Text = "My Blue Label",
Style = myLabelStyle,
};
var content = new ContentPage
{
Content = new StackLayout
{
Children = {
myLabelRed,
myLabelBlue
}
}
};
iOS:
Android:
它似乎是一个 Xamarin Forms 错误 在版本 2.0.0.6482(按预期工作)和 版本 2.2.0.31 之间的某个地方引入(坏了!没有按预期工作)。