ReactiveUI - ViewCell绑定

时间:2017-02-01 17:40:38

标签: xamarin.forms reactiveui

我正在尝试重现ReactiveUI的101 example并将其调整为Xamarin Forms

我使用自定义ViewCell但不能以ReactiveUI方式绑定它:

public partial class FlickrPhotoCell : ViewCell, IViewFor<FlickrPhotoModel>
{
    public FlickrPhotoCell()
    {
        InitializeComponent();
        this.Bind(ViewModel, vm => vm.Title, v => v.TitleLabel.Text);
        this.Bind(ViewModel, vm => vm.Description, v => v.DescriptionLabel.Text);
        this.Bind(ViewModel, vm => vm.Url, v => v.UrlImage.Source, null, new CustomConverter());
    }

    //The rest of the code below is plumbing:

    public static readonly BindableProperty ViewModelProperty = BindableProperty.Create(nameof(ViewModel), typeof(FlickrPhotoModel), typeof(FlickrPhotoCell));

    public FlickrPhotoModel ViewModel
    {
        get { return (FlickrPhotoModel)GetValue(ViewModelProperty); }
        set { SetValue(ViewModelProperty, value); }
    }

    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = (FlickrPhotoModel)value; }
    }
}

如果我以通常的Xamarin Forms方式执行它,它可以正常工作:

public FlickrPhotoCell()
    {
        InitializeComponent();
        TitleLabel.SetBinding<FlickrPhotoModel>(Label.TextProperty, x => x.Title);
        DescriptionLabel.SetBinding<FlickrPhotoModel>(Label.TextProperty, x => x.Description);
        UrlImage.SetBinding<FlickrPhotoModel>(Image.SourceProperty, x => x.Url);
    }

1 个答案:

答案 0 :(得分:1)

我认为一个区别是Xamarin Forms方式使用BindingContext而ReactiveUI方式纯粹使用ViewModel。所以我的第一个猜测是ViewModel属性没有设置。也许尝试将BindingContext绑定到ViewModel?或者覆盖OnBindingContext更改并在那里设置ViewModel以查看是否有效