我正在尝试重现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);
}
答案 0 :(得分:1)
我认为一个区别是Xamarin Forms方式使用BindingContext而ReactiveUI方式纯粹使用ViewModel。所以我的第一个猜测是ViewModel属性没有设置。也许尝试将BindingContext绑定到ViewModel?或者覆盖OnBindingContext更改并在那里设置ViewModel以查看是否有效