我正在尝试复制101例 of ReactiveUI并使其适应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);
}发布于 2017-02-01 19:03:32
我认为一个不同之处是,Xamarin方法使用的是BindingContext,而ReactiveUI方法使用的是纯ViewModel。所以我的第一个猜测是ViewModel属性没有被设置。也许试着将BindingContext绑定到ViewModel?或者重写OnBindingContext,更改并在那里设置ViewModel,以查看它是否有效。
https://stackoverflow.com/questions/41986169
复制相似问题