我试图将View中的(非组件)属性绑定到View Model中的属性。这是简化的代码。
查看:
public partial class InputFormCell : MvxTableViewCell
{
[Outlet]
UIKit.UITextField Text {get; set;}
public bool SelectOnFocus {get; set;}
public bool TextHasFocus {get; set;}
public InputFormCell(IntPtr handle) : base(handle)
{
this.DelayBind(() =>
{
this.Text.EditingDidBegin += (sender, e) =>
{
this.BecomeFirstResponder();
if(SelectOnFocus)
{
Text.PerformSelector(new Selector("selectAll"), null, 0.0f);
}
TextHasFocus = true;
};
this.Text.EditingDidEnd += (sender, e) =>
{
TextHasFocus = false;
};
var set = this.CreateBindingSet<InputFormCell, InputViewModel>();
set.Bind(Text).To(vm => vm.Text);
set.Bind(this).For(vc => vc.TextHasFocus).To(vm => vm.TextHasFocus).TwoWay();
set.Apply();
}
}
}
视图模型:
[ImplementPropertyChanged]
public class InputViewModel : MvxViewModel
{
public string Text {get; set;}
public event Action<string> TextGetsFocus;
public bool TextHasFocus { get; set; }
protected virtual void OnTextHasFocusChanged()
{
if (TextHasFocus) // Problem: Never gets to here.
{
TextGetsFocus?.Invoke(Text);
}
// ..
}
}
ImplementPropertyChanged
属性是Fody(我认为)的一部分,但无论如何,OnTextHasFocusChanged
在InputViewModel.TextHasFocus
直接更新时会被正确触发。但它并没有通过我所拥有的这种约束来触发。
问题:为什么OnTextHasFocusChanged
更新时不会触发InputFormCell.TextHasFocus
?