当尝试绑定Bindable Property时,可能会遇到绑定失败的问题。的确,启用Prism后,在Xamarin Forms中添加Bindable Property有点棘手,但有以下几种方法可以解决此问题。
public static readonly BindableProperty IsSelectedProperty =
BindableProperty.Create(nameof(IsSelected), typeof(bool), typeof(MyViewModel), false);
public bool IsSelected
{
get { return (bool)GetValue(IsSelectedProperty); }
set { SetValue(IsSelectedProperty, value); }
}
xmlns:vm="clr-namespace:MyApp.ViewModels;assembly=MyApp"
static MyControl()
{
IsSelectedProperty.Changed += (bindable, oldvalue, newvalue) =>
{
// 在绑定属性更改时进行操作
};
}
只需将以上任一方法应用于您的代码即可成功解决Bindable Property绑定失败问题。