Child 属性更新调用它是 parent 的 `OnPropertyChanged`

Child properties update calling it's parent's `OnPropertyChanged`

我正在尝试创建一个 XF 组件,其某些属性属于从 BindableObject 继承的类型。为了说明,我有 class Shadowdouble RadiusColor ShadowColor 属性和 class MyBoxText,它们有 bool IsLoadingShadow Ghost 个属性。

我的 View 和它的 Bindings 按预期工作,但我的自定义渲染器有问题:

当我更改 Ghost 属性时,我需要重绘整个视图(MyBoxText 控件),例如,以视觉方式更新阴影颜色。

这是一些微博:

类代码:

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' 覆盖没有被调用,并且视图在屏幕上保持旧颜色。

有没有办法将 child 的 'Property Update' 事件传播到 parent 视图 'Property Changed' 或其他实现此目的的方法?

The issue is that when I change the Ghost.ShadowColor property my 'OnElementPropertyChanged' override is not called, and the View stays with the old color on the screen. Is there a way to propagate the child's 'Property Update' event to parent view 'Property Changed' or another way to achieve this?

是的,有办法。由于您的 Shadow 继承自 BindableObject,它实现了 INotifyPropertyChanged 接口。您可以设置通知 ShadowColor 更改:

  1. OnPropertyChanged() 添加到 Shadow.cs 中 ShadowColor 的 Setter:

    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);
                //Notify the ShadowColorProperty Changed
                OnPropertyChanged();
            }
        }
       ...
    }
    
  2. 像这样修改你的MyBoxText.cs

    public class MyBoxText : Label /* It's bindable by inheritance */
    {
     ...
    
        public static readonly BindableProperty GhostProperty = BindableProperty.Create(nameof(Ghost), typeof(Shadow), typeof(MyBoxText), null);
        public Shadow Ghost
        {
            get { return (Shadow)GetValue(GhostProperty); }
            set {
                //register the ShadowColor change event
                value.PropertyChanged += ShadowColor_PropertyChanged;
                SetValue(GhostProperty, value); }
        }
    
        private void ShadowColor_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            //unregister the event
            this.Ghost.PropertyChanged -= ShadowColor_PropertyChanged;
            //set this.Ghost to a new object with new ShadowColor to trigger the OnPropertyChanged
            this.Ghost = new Shadow
            {
                ShadowColor = (sender as Shadow).ShadowColor,
                ShadowRadius = Ghost.ShadowRadius
            };
        }
    }
    

感谢 Elvis 的回答,我明白了。根据他的想法,我做了一些更改以在其他组件上重用它,我现在分享它以防其他人需要这样的东西。

我认为这样使用它我们可以获得更清晰和简单的代码:

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 class:

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);
        }
    }
}