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

如何检查Xamarin.Forms中IsPropertyChanged方法内可绑定属性的设置?

如何解决如何检查Xamarin.Forms中IsPropertyChanged方法内可绑定属性的设置?

我有这段代码根据一个名为IsEnabled的属性标签设置为一种或另一种颜色:

public class LinkLabel : Label
{
    public LinkLabel()
    {
        PropertyChanged += LinkLabel_PropertyChanged;
        SetDynamicResource(Label.TextColorProperty,"LinkLabelColor");
    }

    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,"LinkLabelColor");
            else
                this.SetDynamicResource(Label.TextColorProperty,"LinkLabeldisabledColor");
        }

    }
}

我想做的是添加一个名为IsImportant的属性

  • 因此,如果将其设置为true且IsEnabled = true,则LinkLabelColor将被设置为红色

有人可以给我一些建议吗?我确实知道如何添加诸如可绑定属性之类的东西,但是在这种情况下我不确定如何将其与已有的代码结合起来?

解决方法

添加新属性

public static readonly BindableProperty IsImportantProperty = BindableProperty.Create(nameof(IsImportant),typeof(bool),typeof(CustomLabel),false,/*Can remove property changed as well*/ propertyChanged: IsImportantPropertyChanged);
        
public bool IsImportant
{
   get { return (bool)GetValue(IsImportantProperty); }
   set { SetValue(IsImportantProperty,value); }
}

新方法更新

protected override void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
   if (propertyName == IsEnabledProperty.PropertyName || propertyName == IsImportantProperty.PropertyName)
   {
      if (this.IsImportant && this.IsEnabled)
         this.SetDynamicResource(Label.TextColorProperty,"LinkLabelColor");
      else
         this.SetDynamicResource(Label.TextColorProperty,"LinkLabelDisabledColor");
   }
}
,

其他答案的某些问题不起作用。我重新检查了代码,并得出了效果很好的代码:

<a href="/product/23" id="link_Page">The Text </a>

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