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

如何检查Xamarin.Forms中IsPropertyChanged方法内可绑定属性的设置?,xamarin,xamarin.forms,Xamarin,Xamarin.forms,我有一段代码,根据名为IsEnabled的属性将标签设置为一种或另一种颜色: public class LinkLabel : Label { public LinkLabel() { PropertyChanged += LinkLabel_PropertyChanged; SetDynamicResource(Label.TextColorProperty, "LinkLabelColor"); } pr

我有一段代码,根据名为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");
   }
}

有些问题另一个答案不起作用。我重新研究了代码,并提出了一个很好的解决方案:

public class LinkLabel : Label
{
    public LinkLabel()
    {
        SetDynamicResource(Label.FontFamilyProperty, "Default-Regular");
        SetDynamicResource(Label.FontSizeProperty,   "LabelTextFontSize");
        SetDynamicResource(Label.TextColorProperty,  "LinkLabelColor");
        VerticalOptions = LayoutOptions.CenterAndExpand;
        VerticalTextAlignment = TextAlignment.Center;
    }

    public static readonly BindableProperty IsImportantProperty =
        BindableProperty.Create(nameof(IsImportant), typeof(bool), typeof(LinkLabel), false);

    public bool IsImportant
    {
        get { return (bool)GetValue(IsImportantProperty); }
        set { SetValue(IsImportantProperty, value); }
    }

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

}

你能解释一下你所说的:/*可以删除已更改的属性吗*/propertyChanged:IsImportantPropertyPropertyChanged你是说我可以删除propertyChanged+=LinkLabel\u propertyChanged?我还注意到你的方法只有一个方法名?如果IsEnabled更改,情况如何。我们不需要也跟踪它吗?所以首先有两件事:
IsImportantPropertyPropertyChanged
如果有什么特别的事情要做,如果
IsImportant
发生了变化,你需要有这个,因此不是强制性的:如果您使用此
LinkLabel\u PropertyChanged
,那么您将跟踪每个属性更改,而不仅仅是
IsEnabled
IsImportant
。更新了可添加到LinkLabel类中的方法,方法
LinkLabel\u PropertyChanged
将在代码隐藏中工作,您使用的控件不在类中如果我已覆盖void OnPropertyChanged,我是否仍需要调用base.OnPropertyChanged?@Alan2我不确定您是否可以检查,是否有任何东西不起作用?如果不是,那么你可以接受答案