ReactiveTableViewSource<TSource> 是如何实现的?工作? [英] How does ReactiveTableViewSource&lt;TSource&gt; work?

查看:37
本文介绍了ReactiveTableViewSource<TSource> 是如何实现的?工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的应用中,我们使用 ReactiveUI 来遵循 MVVM 模式.在一个视图中,我们想要显示一个 UITableView.数据通常传递给 UITableView.Source 但对于 ReactiveUI 我们使用 ReactiveTableViewSource.

In our app we use ReactiveUI to follow the MVVM pattern. In one view we want to show a UITableView. Data are usually passed to a UITableView.Source but with ReactiveUI we use ReactiveTableViewSource<TSource>.

我不明白的是如何将在 ReactiveList 中的视图模型中获得的数据绑定到 ReactiveTableViewSource.

What I don't understand is how I can bind my data that I got in my view model in a ReactiveList to the ReactiveTableViewSource.

现在我们在 UIView 中创建了一个表格,如下所示:

What we now have is that we create a table inside a UIView like this:

Table = new UITableView(UIScreen.MainScreen.Bounds);
Table.Source = new TableSource(Table, MyReactiveList, (NSString) CellIdentifier, _cellHeight, cell => Debug.WriteLine(cell));

顺便说一句:单元格动作是做什么用的?

Btw: What is the cell action used for?

此外,我们有一个如下所示的 Table 源类:

Furthermore we have a Table source class that looks like this:

internal sealed class TableSource : ReactiveTableViewSource<MyListObject>
{
  public TableSource(UITableView tableView, IReactiveNotifyCollectionChanged<MyListObject> collection, NSString cellKey, float sizeHint, Action<UITableViewCell> initializeCellAction = null) : base(tableView, collection, cellKey, sizeHint, initializeCellAction)

在我的视图模型中,我有一个正在更新我的 ReactiveList 的服务.它看起来像这样:

In my view model I have a service that is updating my ReactiveList. It looks like this:

public sealed class MyService : ReactiveObject, IMyService
{
  public IReactiveList<MyListObject> MyReactiveList { get; }

  public async Task UpdateMyReactiveListAsync() {//...}

我在哪里将表源绑定到 ReactiveList?我在哪里订阅活动?有没有我可能遗漏的文档或示例代码?

Where do I bind the table source to the ReactiveList? Where do I subscribe for events? Is there any documentation or example code I might have missed?

推荐答案

使用 ReactiveTableViewSource 非常简单:

Working with ReactiveTableViewSource is quite easy:

只需将 List 与您的 UITableView 连接起来

Just connect the List with your UITableView

var tableView = new UITableView ();

// Bind the List agains the table view
// SampleObject is our model and SampleCell the cell
ViewModel.WhenAnyValue (vm => vm.Items).BindTo<SampleObject, SampleCell> (tableView, 46, cell => cell.Initialize());

然后创建一个自定义单元格,您可以在其中将模型数据与单元格绑定.

Then create a custom cell where you bind the model data against the cell.

public class SampleCell : ReactiveTableViewCell, IViewFor<SampleObject>
{
    public SampleCell () : base() { }
    public SampleCell (IntPtr handle) : base(handle) { }

    private SampleObject _viewModel;
    public SampleObject ViewModel
    {
        get { return _viewModel; }
        set { this.RaiseAndSetIfChanged (ref _viewModel, value); }
    }

    object IViewFor.ViewModel
    {
        get { return ViewModel; }
        set { ViewModel = value as SampleObject; }
    }

    public void Initialize()
    {
        this.WhenAnyValue (v => v.ViewModel.Name).BindTo (
            this,
            v => v.TextLabel.Text);
    }
}

您可以在此处找到一个引人注目的示例:https://github.com/reicheltp/ReactiveTableViewSource-Sample

A compelling example you can find here: https://github.com/reicheltp/ReactiveTableViewSource-Sample

更新 2016/03/09:最好在单独的 Initialize 方法中进行绑定以防止多次调用.

Update 2016/03/09: Better do binding in a separated Initialize method to prevent multiple calls.

如果你有更多问题可以在推特上问我:@reicheltp

If you have more questions you can ask me on twitter: @reicheltp

这篇关于ReactiveTableViewSource<TSource> 是如何实现的?工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆