MvvmCross更新View中的绑定属性,而不是传播到ViewModel中的属性

时间:2017-10-04 06:18:10

标签: c# xamarin.ios mvvmcross

我试图将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(我认为)的一部分,但无论如何,OnTextHasFocusChangedInputViewModel.TextHasFocus直接更新时会被正确触发。但它并没有通过我所拥有的这种约束来触发。

问题:为什么OnTextHasFocusChanged更新时不会触发InputFormCell.TextHasFocus

0 个答案:

没有答案