c# - BindableProperty 通知属性改变

标签 c# xamarin xamarin.forms

在自定义 ContentView 上,我创建了一个 BindableProperty,如下所示

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

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

最佳答案

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

BindingPropertyChangedDelegate<in TPropertyType>(BindableObject bindable, TPropertyType oldValue, TPropertyType newValue);

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

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

这显示了在标记属性的共享代码中捕获属性更改的正确方法。如果您在 native 项目中有自定义渲染器,它的 OnElementPropertyChanged 事件触发,将“StrokeColor”作为 PropertyName,无论是否将此 propertyChanged 委托(delegate)提供给 BindableProperty 定义。

关于c# - BindableProperty 通知属性改变,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38498152/

相关文章:

c# - 在参数中将 list<string> 转换为 IEnumerable<guid>

c# - Xamarin - Entity Framework - SqLite - GetItemsAsync : unable to open database file

android - 如何全局处理MonoDroid未捕获的异常并防止应用崩溃

ios - Monotouch/Xamarin 更改 iPad 应用程序的表格行高

xamarin.forms - 图像色调按钮 Android Xamarin Forms

c# - 一个过程完成所需的时间

c# - 什么时候应该覆盖 OnEvent 而不是在继承时订阅事件

c# - 如何使用 Xamarin.Forms.iOS 更改呈现器 View 中的 UIPickerView 文本颜色?

c# - 通过 HtmlDocument.All 集合的迭代在引用的样式表处停止?

c# - 如何使用 MVVM C#​​ 添加到 ObservableCollection