【发布时间】:2020-11-05 16:35:28
【问题描述】:
我正在使用的模板中有此代码,但由于此消息它无法工作:
我了解为什么会出现该消息,但我不知道如何解决此问题,以便标签颜色取决于 isEnabled 的状态。
namespace Templates
{
public class LinkLabel : Label
{
public LinkLabel()
{
this.SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
this.SetDynamicResource(Label.FontSizeProperty, "LabelTextFontSize");
this.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
VerticalTextAlignment = TextAlignment.Center;
VerticalOptions = LayoutOptions.CenterAndExpand;
}
public static BindableProperty IsEnabledProperty =
BindableProperty.Create(nameof(IsDisabled), typeof(bool), typeof(LinkLabel), default(bool), propertyChanged: IsEnabledPropertyPropertyChanged);
public bool IsDisabled
{
get { return (bool)GetValue(IsEnabledProperty); }
set { SetValue(IsEnabledProperty, value); }
}
private static void IsEnabledPropertyPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
if ((bool)newValue)
{
LinkLabel label = bindable as LinkLabel;
label.SetDynamicResource(Label.TextColorProperty, "LinkLabelDisabledColor");
}
else
{
LinkLabel label = bindable as LinkLabel;
label.SetDynamicResource(Label.TextColorProperty, "LinkLabelColor");
}
}
}
}
【问题讨论】:
-
喜欢 BoolToColor converter?所以你可以在xaml中设置:color="{Binding boolvalue, Converter={StaticResource MyConverter}}"
-
并非如此。我正在寻找基于 isEnabled 属性值设置颜色的 C# 解决方案。
-
"public static BindableProperty MyEnabledProperty", "BindingContext = this;"并在 xaml 集中:IsEnable="{Binding MyEnabled}"。像这样?
标签: xamarin xamarin.forms