以下是我定义属性的方式,它适用于string
或bool
等其他类型的属性,但它不适用于Command
类型:
public static readonly BindableProperty ClickedProperty = BindableProperty.Create(
nameof(Clicked),
typeof(Command),
typeof(MyCustomControl),
default(Command),
BindingMode.OneWay,
propertyChanging: (bindable, oldValue, newValue) => {
var control = bindable as MyCustomControl;
if (!control.GestureRecognizers.Contains(gesture))
{
control.GestureRecognizers.Add(gesture);
}
}
);
public Command Clicked
{
get { return (Command)GetValue(ClickedProperty); }
set { SetValue(ClickedProperty, value); }
}
我尝试搜索它为Xamarin.Forms.Button
课程完成的操作,但它似乎使用了IButtonController
界面,不适合公众使用。
更新
我将属性更改为event
类型,现在当我从XAML表单绑定属性时调用访问器,但是propertyChanged
和propertyChanging
都没有被调用。
public event EventHandler Clicked
{
add
{
lock (gesture)
{
gesture.Tapped += value;
}
}
remove
{
lock (gesture)
{
gesture.Tapped -= value;
}
}
}