MVVMCross-绑定到集合

时间:2019-04-01 19:18:46

标签: c# ios xamarin mvvm mvvmcross

我有一个带有属性List<MyModel> Items的视图模型。设置Items集合后,我向iOS中的ViewController触发了一个事件。然后ViewController接收事件并遍历Items以创建以编程方式构建的包含各种标签和文本字段的UI。

我想做的是将UITextField绑定到Items内MyModel上的属性,但是在MVVMCross中看不到做到这一点的方法。有什么建议么?

下面是一些简化的代码,以进一步概述我的问题:

public class MyModel : MvxNotifyPropertyChanged
{
    public string SomeProperty
    {
        get => return this.someProperty;
        set => this.SetProperty(ref this.someProperty, value);
    }
}
public class MyViewModel : MvxViewModel
{
    public List<MyModel> Items { get; set; }

    public event EventHandler onItemsLoaded;

    public void Initialize()
    {
        this.Items = this.someService.GetItems();
        this.onItemsLoaded.Invoke(this, EventArgs.Empty);
    }

}
public class MyViewController : MvxViewController<MyViewModel>
{
    public override void ViewDidLoad()
    {
        this.ViewModel.onItemsLoaded += this.HandleItemsLoaded;
    }

    private void HandleItemsLoaded(object sender, EventArgs args)
    {
        foreach (var item in this.ViewModel.Items)
        {
            var textField = new UITextField();

            // TODO: How to bind textField.Text to item.SomeProperty??

            this.View.AddSubview(textField);
        }
    }
}

0 个答案:

没有答案