子属性更新调用它的父亲的'OnPropertyChanged`

时间:2018-02-26 16:33:18

标签: c# xamarin binding xamarin.android custom-controls

我试图创建一个XF组件,其某些属性属于继承自BindableObject的类型。为了说明,我的课程Shadow包含double RadiusColor ShadowColor属性以及课程MyBoxText,其中包含bool IsLoadingShadow Ghost属性。

我的View及其Bindings按预期工作,但我的自定义渲染器存在问题:

当我更改Ghost属性时,我需要重绘整个视图(MyBoxText控件),以便直观地更新阴影颜色。

这里有一些mcve:

课程代码:

public class MyBoxText : Label /* It's bindable by inheritance */
{
    #region Properties
    public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create(nameof(IsLoading), typeof(bool), typeof(MyBoxText), false) ;
    public bool IsLoading
    {
        get { return (bool)GetValue(IsLoadingProperty); }
        set { SetValue(IsLoadingProperty, value); }
    }

    public static readonly BindableProperty GhostProperty = BindableProperty.Create(nameof(Ghost), typeof(Shadow), typeof(MyBoxText), null) ;
    public Shadow Ghost
    {
        get { return (Shadow)GetValue(GhostProperty); }
        set { SetValue(GhostProperty, value); }
    }
    #endregion
}

public class Shadow : BindableObject /* It's explictly bindable */
{
    #region Properties
    public static readonly BindableProperty ShadowColorProperty = BindableProperty.Create(nameof(ShadowColor), typeof(Color), typeof(Shadow), Color.Black) ;
    public Color ShadowColor
    {
        get { return (Color)GetValue(ShadowColorProperty); }
        set { SetValue(ShadowColorProperty, value); }
    }

    public static readonly BindableProperty ShadowRadiusProperty = BindableProperty.Create(nameof(ShadowRadius), typeof(double), typeof(Shadow), 20) ;
    public double ShadowRadius
    {
        get { return (double)GetValue(ShadowRadiusProperty); }
        set { SetValue(ShadowRadiusProperty, value); }
    }
    #endregion

    public Shadow()
    {

    }
}

我的渲染器代码是这样的:

public class MyBoxText : LabelRenderer
{
    public MyBoxText()
    {
        SetWillNotDraw(false);
    }

    public override void Draw(Canvas canvas)
    {
        MyBoxText myView = (MyBoxText)this.Element;

        // Some drawing logic
    }

    protected override void OnElementPropertyChanged(object sender, PropertyChangedEventArgs e)
    {
        base.OnElementPropertyChanged(sender, e);

        if (e.PropertyName == MyBoxText.IsLoadingProperty.PropertyName  ||
            e.PropertyName == MyBoxText.GhostProperty.PropertyName )
            Invalidate();
    }
}

问题在于,当我更改Ghost.ShadowColor属性时,我的' OnElementPropertyChanged'不调用覆盖,并且视图在屏幕上保留旧颜色。

有没有办法传播孩子的“物业更新”?事件到父视图'属性已更改'或另一种方法来实现这一目标?

1 个答案:

答案 0 :(得分:1)

感谢猫王的回答我得到了它。基于他的想法,我做了一些改变,将其重新用于其他组件,我现在正在分享它,以防其他人需要这样的东西。

我认为以这种方式使用它我们可以获得更简洁的代码:

public class MyBoxText : Label /* It's bindable by inheritance */
{
    // Added this as private property
    private ChangingPropagator changingPropagator;
    private ChangingPropagator ChangingPropagator
    {
        get
        {
            if (changingPropagator == null)
                changingPropagator = new ChangingPropagator(this, OnPropertyChanged, nameof(Shadow.ShadowColor), nameof(Shadow.ShadowRadius));

            return changingPropagator;
        }
    }

    #region Properties
    public static readonly BindableProperty IsLoadingProperty = BindableProperty.Create(nameof(IsLoading), typeof(bool), typeof(MyBoxText), false) ;
    public bool IsLoading
    {
        get { return (bool)GetValue(IsLoadingProperty); }
        set { SetValue(IsLoadingProperty, value); }
    }

    public static readonly BindableProperty GhostProperty = BindableProperty.Create(nameof(Ghost), typeof(Shadow), typeof(MyBoxText), null) ;
    public Shadow Ghost
    {
        // Here I use the ChangingPropagator's Getter and Setter instead of the deafult ones:
        get { return ChangingPropagator.GetValue<Shadow>(GhostProperty); }
        set { ChangingPropagator.SetValue(GhostProperty,ref value); }
    }
    #endregion
}

这是ChangingPropagator类:

public class ChangingPropagator
{
    string[] listenedProperties = new string[0];
    Action<string> changesNotifyer = null;
    BindableObject propagationRootObject = null;
    List<KeyValuePair<string, object>> propagationProperties = new List<KeyValuePair<string, object>>();

    public ChangingPropagator(BindableObject bindableObject, Action<string> onPropertyChangedMethod, params string[] propertyToListenTo)
    {
        changesNotifyer = onPropertyChangedMethod;
        propagationRootObject = bindableObject;
        listenedProperties = propertyToListenTo ?? listenedProperties;

        // ToDo: Add some consistency checks
    }

    public void AddPropertyToListenTo(params string[] propertyName)
    {
        listenedProperties = listenedProperties.Union(propertyName).ToArray();
    }

    // I need handle it here too 'cause when I use the child `Ghost` property coming from XAML binding, it didn't hit the `set` method
    public T GetValue<T>(BindableProperty property)
    {
        var value = propagationRootObject?.GetValue(property);

        if (value != null)
        {
            INotifyPropertyChanged bindableSubObject = (value as INotifyPropertyChanged);

            if (bindableSubObject != null)
            {
                bindableSubObject.PropertyChanged -= PropagatorListener;
                bindableSubObject.PropertyChanged += PropagatorListener;

                if (!propagationProperties.Any(a => a.Key == property.PropertyName))
                    propagationProperties.Add(new KeyValuePair<string, object>(property.PropertyName, value));
            }
        }

        return (T)value;
    }

    public void SetValue<T>(BindableProperty property, ref T value)
    {
        var oldValue = propagationRootObject?.GetValue(property);

        if (oldValue != null)
        {
            INotifyPropertyChanged bindableSubObject = (value as INotifyPropertyChanged);

            if (bindableSubObject != null)
                bindableSubObject.PropertyChanged -= PropagatorListener;
        }

        if (value != null)
        {
            INotifyPropertyChanged bindableSubObject = (value as INotifyPropertyChanged);
            if (bindableSubObject != null)
            {
                bindableSubObject.PropertyChanged += PropagatorListener;

                propagationProperties.RemoveAll(p => p.Key == property.PropertyName);
                propagationProperties.Add(new KeyValuePair<string, object>(property.PropertyName, value));
            }
        }

        propagationRootObject.SetValue(property, value);
    }

    private void PropagatorListener(object sender, PropertyChangedEventArgs e)
    {
        if (listenedProperties?.Contains(e.PropertyName) ?? true)
            PropagationThrower(sender);
    }

    private void PropagationThrower(object sender)
    {
        if (propagationProperties.Any(p => p.Value == sender))
        {
            var prop = propagationProperties.FirstOrDefault(p => p.Value == sender);
            changesNotifyer?.Invoke(prop.Key);
        }
    }
}