BindableProperty通知属性已更改

Las*_*sen 3 c# xamarin xamarin.forms

在一个风俗ContentView我创建了BindableProperty这样的

public static readonly BindableProperty StrokeColorProperty = 
    BindableProperty.Create("StrokeColor", 
                            typeof(Color), 
                            typeof(SignaturePadView), 
                            Color.Black, 
                            BindingMode.TwoWay);
Run Code Online (Sandbox Code Playgroud)

但是我必须通知属性更改,因为必须在自定义渲染器中读取属性,我该怎么做?
如果我在上将其设置PropertyChangedBindableProperty静态方法,则不能从这种方式获取它:(

irr*_*eal 6

BindableProperty的PropertyChanged事件处理程序确实将是静态的,但是其上的输入参数是

BindingPropertyChangedDelegate<in TPropertyType>(BindableObject bindable, TPropertyType oldValue, TPropertyType newValue);
Run Code Online (Sandbox Code Playgroud)

如您所见,第一个输入参数将是BindableObject。您可以安全地将可绑定对象强制转换为自定义类,并获取属性已更改的实例。像这样:

public static readonly BindableProperty StrokeColorProperty = 
    BindableProperty.Create("StrokeColor", 
                            typeof(Color), 
                            typeof(SignaturePadView), 
                            Color.Black, 
                            BindingMode.TwoWay,
propertyChanged: (b, o, n) =>
                {
                    var spv = (SignaturePadView)b;
                    //do something with spv
                    //o is the old value of the property
                    //n is the new value
                });
Run Code Online (Sandbox Code Playgroud)

这显示了正确的方法来捕获对属性进行了缩放的共享代码中的属性更改。如果您在本机项目中有一个自定义渲染器,则将触发OnElementPropertyChanged事件,并以“ StrokeColor”作为PropertyName,无论是否将此propertyChanged委托提供给BindableProperty定义。