首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ReactiveUI - ViewCell结合

ReactiveUI - ViewCell结合
EN

Stack Overflow用户
提问于 2017-02-01 17:40:38
回答 1查看 385关注 0票数 2

我正在尝试复制101例 of ReactiveUI并使其适应Xamarin Forms

我使用自定义的ViewCell,但不能以ReactiveUI的方式绑定它:

代码语言:javascript
复制
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方式做这件事的话,效果会很好:

代码语言:javascript
复制
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);
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-01 19:03:32

我认为一个不同之处是,Xamarin方法使用的是BindingContext,而ReactiveUI方法使用的是纯ViewModel。所以我的第一个猜测是ViewModel属性没有被设置。也许试着将BindingContext绑定到ViewModel?或者重写OnBindingContext,更改并在那里设置ViewModel,以查看它是否有效。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41986169

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档