创建
BehaviorBase
来更新您的 behavior
。
public class BehaviorBase<T> : Behavior<T> where T : BindableObject
{
#region Properties
public T AssociatedObject
{
get;
private set;
}
#endregion
#region NormalMethods
private void OnBindingContextChanged(object sender, EventArgs e)
{
OnBindingContextChanged();
}
#endregion
#region Overrides
protected override void OnAttachedTo(T bindable)
{
base.OnAttachedTo(bindable);
AssociatedObject = bindable;
if (bindable.BindingContext != null)
{
BindingContext = bindable.BindingContext;
}
bindable.BindingContextChanged += OnBindingContextChanged;
}
protected override void OnDetachingFrom(T bindable)
{
base.OnDetachingFrom(bindable);
bindable.BindingContextChanged -= OnBindingContextChanged;
AssociatedObject = null;
}
protected override void OnBindingContextChanged()
{
base.OnBindingContextChanged();
BindingContext = AssociatedObject.BindingContext;
}
#endregion
}
让你的
MaskedBehavior
继承BehaviorBase
。
public class MaskedBehavior : BehaviorBase<Entry>
{
……..
}
和你的用法一样。
我已经将整个项目上传到GitHub上,大家可以下载参考。 https://github.com/WendyZang/Test/tree/master/Behavior
能否分享一下完整的解决方案(github项目已经不存在了)