微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何根据isEnabled是true还是false更改标签的颜色?

如何解决如何根据isEnabled是true还是false更改标签的颜色?

我在正在使用的模板中包含此代码,但由于出现此消息而无法正常工作

ErrorMessage

我了解为什么会显示此消息,但我不知道如何解决此问题,以便标签颜色取决于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");
            }
        }
    }
}

解决方法

如果我们检查类 VisualElement 的源代码,我们会发现已经存在一个名为 IsEnabledProperty 的可绑定属性。由于 VisualElement 是大多数UI元素的超类。因此,两个IsEnabledProperty之间将存在歧义。

enter image description here

解决方案1:

最简单的方法是修改可绑定属性的名称,例如 IsDisabledProperty

解决方案2:

由于Label中已经存在 IsEnabledProperty 。我们可以在xaml中设置该值,并在后面的代码中处理逻辑,例如

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;

        this.PropertyChanged += LinkLabel_PropertyChanged;

    }

    private void LinkLabel_PropertyChanged(object sender,PropertyChangedEventArgs e)
    {
       
        if(e.PropertyName== "IsEnabled")
        {
            var label = sender as LinkLabel;
            var newValue = label.IsEnabled;

            if ((bool)newValue)
            {
              
                this.SetDynamicResource(Label.TextColorProperty,"LinkLabelDisabledColor");
            }
            else
            {

                this.SetDynamicResource(Label.TextColorProperty,"LinkLabelColor");
            }

        }

    }

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。