需要在绑定对象的可绑定属性(BindableProperty)的get方法中捆绑一个委托,然后在属性更改时调用该委托以更新绑定的对象。以下是一个解决方法的示例代码:
public class MyBindableObject : BindableObject
{
public static readonly BindableProperty MyProperty =
BindableProperty.Create(nameof(MyProperty), typeof(string), typeof(MyBindableObject), null, BindingMode.TwoWay, null, OnMyPropertyChanged);
private static void OnMyPropertyChanged(BindableObject bindable, object oldValue, object newValue)
{
var obj = (MyBindableObject) bindable;
obj.MyDelegate?.Invoke(newValue);
}
public string MyBindableString
{
get => (string) GetValue(MyProperty);
set => SetValue(MyProperty, value);
}
public Action MyDelegate { get; set; }
}
在这个示例中,MyBindableObject包含一个名为MyBindableString的可绑定属性,并且还公开了一个名为MyDelegate的委托属性。在MyProperty属性的get方法中,我们使用了一个静态OnMyPropertyChanged方法,该方法检查了该属性的新值并调用了MyDelegate委托,以便更新所有绑定到MyBindableObject的对象。通过在MyBindableObject实例上设置MyDelegate属性,我们可以在属性更改时更新任意数量的对象。