【发布时间】:2018-08-06 04:07:53
【问题描述】:
我正在尝试创建一个 XF 组件,它的某些属性是继承自 BindableObject 的类型。为了说明,我有一个类Shadow 具有double Radius 和Color ShadowColor 属性和一个类MyBoxText,它具有一个bool IsLoading 和一个Shadow 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”覆盖没有被调用,并且视图在屏幕上保持旧颜色。
有没有办法将孩子的“属性更新”事件传播到父视图“属性已更改”或其他方式来实现这一点?
【问题讨论】:
标签: c# xamarin binding xamarin.android custom-controls