2
我試圖重現ReactiveUI的101 example並使其適應Xamarin Forms
。ReactiveUI - ViewCell綁定
我使用自定義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);
}
能否請您添加查看XAML中,尤其是一個使用ListView? – Giusepe
一個區別,我認爲是,Xamarin窗體而ReactiveUI的方法是使用純粹的視圖模型的方法是使用的BindingContext。所以我的第一個猜測是ViewModel屬性沒有設置。也許嘗試將BindingContext綁定到ViewModel?或重寫OnBindingContext更改並設置ViewModel只是爲了看看是否可行 –
這聽起來像一個明智的評論。我現在就試試! –