2017-02-01 68 views
2

我試圖重現ReactiveUI的101 example並使其適應Xamarin FormsReactiveUI - 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); 
    } 
+0

能否請您添加查看XAML中,尤其是一個使用ListView? – Giusepe

+0

一個區別,我認爲是,Xamarin窗體而ReactiveUI的方法是使用純粹的視圖模型的方法是使用的BindingContext。所以我的第一個猜測是ViewModel屬性沒有設置。也許嘗試將BindingContext綁定到ViewModel?或重寫OnBindingContext更改並設置ViewModel只是爲了看看是否可行 –

+0

這聽起來像一個明智的評論。我現在就試試! –

回答

1

一個不同之處,我認爲是, Xamarin Forms的方式是使用BindingContext,而ReactiveUI的方式是純粹使用ViewModel。所以我的第一個猜測是ViewModel屬性沒有設置。也許嘗試將BindingContext綁定到ViewModel?或覆蓋OnBindingContext改變和設定的視圖模型那裏只是爲了看看是否能工程