Xamarin.forms ReactiveUI-ViewCell绑定

Xamarin.forms ReactiveUI-ViewCell绑定,xamarin.forms,reactiveui,Xamarin.forms,Reactiveui,我正试图复制ReactiveUI的功能,并将其应用于Xamarin表单 我使用自定义的ViewCell,但无法通过ReactiveUI方式将其绑定: public partial class FlickrPhotoCell : ViewCell, IViewFor<FlickrPhotoModel> { public FlickrPhotoCell() { InitializeComponent(); this.Bind(ViewMod

我正试图复制ReactiveUI的功能,并将其应用于
Xamarin表单

我使用自定义的
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; }
    }
}
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);
    }

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

能否添加您的视图Xaml,尤其是带有ListView的视图?我认为一个区别是Xamarin Forms方式使用BindingContext,而ReactiveUI方式仅使用ViewModel。所以我的第一个猜测是ViewModel属性没有设置。也许可以尝试将BindingContext绑定到ViewModel?或者重写OnBindingContext已更改,并在其中设置ViewModel,以查看该选项是否有效,这听起来像是一条合理的注释。我现在就试试!我认为这样做的目的是使RxUI绑定引擎尽可能独立于Xamarin形式,从而使其更能抵御破坏性更改。对于这样的事情,通常我只是将转换器的逻辑移动到一个可重用的地方,然后我可以从两个地方调用它。老实说,当我必须做转换器的事情时,我更喜欢使用RxUI绑定,因为在内联函数上指定这些转换比转换器的仪式要好得多:-)IMHO。此外,这一切都在一个地方,而且很容易理解。必须手动分配到
ViewModel
的问题也多次困扰着我,因此我提出了一个问题来解决这个问题: