从哪个类inheritance绑定的表源
我使用Xamarin和mvvmcross,以及最终绑定到可观察集合的表格。
这个video是非常丰富的如何创build自定义单元格,但似乎是过时的。 大约42分钟,Stuart为他的表格创build了一个源自MvxSimpleBindableTableSource
的数据源,但是这个类似乎并不存在,或者至less我找不到它。 那么用mvvmcross绑定到UITableView的“最佳”方法是什么?
另外,我在一个普通的MvxViewController中使用了一个UITableView,因为我似乎无法得到MvxTableViewController来处理一个xib, 这个问题似乎表明目前是不可能的。
可用的v3表格源是:
抽象类
- MvxBaseTableViewSource
- 仅基本function
- 没有
ItemsSource
– 一般不直接使用
- MvxTableViewSource.cs
- 从Basetableinheritance,并添加用于数据绑定的
ItemsSource
- inheritance类只需要实现
protected abstract UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item);
- 从Basetableinheritance,并添加用于数据绑定的
具体类
- MvxStandardTableViewSource.cs
- inheritance自
MvxTableViewSource
- 通过
UITableViewCellStyle
提供“标准iPhone单元格types” - 在这些你可以绑定
TitleText
,DetailText
,ImageUrl
和(有些戏弄)配件
- inheritance自
- MvxSimpleTableViewSource.cs
- inheritance自
MvxTableViewSource
- 为集合中的所有项目提供单个单元格types – 通过
ctor
string nibName
- 在这些细胞内,你可以绑定你喜欢的东西 – 看video(稍后)
- inheritance自
- MvxActionBasedTableViewSource.cs – 提供了一些
Func<>
样式钩子,允许您实现GetOrCreateCellFor
而不从MvxTableViewSource
inheritance新的类
一般我使用:
- 在演示中:
- 一个
MvxStandardTableViewSource
– 因为我得到一个列表,而不必创build一个自定义的单元格
- 一个
- 在实际的代码中:
- 一个
MvxSimpleTableViewSource
当我只需要一个单元格types - 当我需要多个单元格types时,从
MvxTableViewSource
inheritance的自定义类 – 例如参见下文
- 一个
具有多个单元格types的常规TableSource通常看起来像PolymorphicListItemTypesView.cs :
public class PolymorphicListItemTypesView : MvxTableViewController { public PolymorphicListItemTypesView() { Title = "Poly List"; } public override void ViewDidLoad() { base.ViewDidLoad(); var source = new TableSource(TableView); this.AddBindings(new Dictionary<object, string> { {source, "ItemsSource Animals"} }); TableView.Source = source; TableView.ReloadData(); } public class TableSource : MvxTableViewSource { private static readonly NSString KittenCellIdentifier = new NSString("KittenCell"); private static readonly NSString DogCellIdentifier = new NSString("DogCell"); public TableSource(UITableView tableView) : base(tableView) { tableView.RegisterNibForCellReuse(UINib.FromName("KittenCell", NSBundle.MainBundle), KittenCellIdentifier); tableView.RegisterNibForCellReuse(UINib.FromName("DogCell", NSBundle.MainBundle), DogCellIdentifier); } public override float GetHeightForRow(UITableView tableView, NSIndexPath indexPath) { return KittenCell.GetCellHeight(); } protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item) { NSString cellIdentifier; if (item is Kitten) { cellIdentifier = KittenCellIdentifier; } else if (item is Dog) { cellIdentifier = DogCellIdentifier; } else { throw new ArgumentException("Unknown animal of type " + item.GetType().Name); } return (UITableViewCell) TableView.DequeueReusableCell(cellIdentifier, indexPath); } } }
这个video是非常丰富的如何创build自定义单元格,但似乎是过时的
它只是在Xamarin 2.0和V3之前制作的,但是原理非常相似。
该文章的代码已更新 – 请参阅https://github.com/slodge/MvvmCross-Tutorials/tree/master/MonoTouchCellTutorial
除此之外:
-
在N + 1系列中有大量的表格使用演示 – 在http://mvvmcross.wordpress.com索引
- N = 2和N = 3是非常基本的
- N = 6和N = 6.5包括一个图书清单(一个好地方开始)
- N = 11包括收集意见
- N = 12到N = 17用数据库中的列表/表格创build一个大型应用程序
-
“使用集合”示例有相当多的表和列表代码 – https://github.com/slodge/MvvmCross-Tutorials/tree/master/Working%20With%20Collections
-
Evolve演示文稿中使用表格 – http://xamarin.com/evolve/2013#session-dnoeearf
-
还有其他样本可用 – 请参阅https://github.com/slodge/MvvmCross-Tutorials/ (或在GitHub上searchmvvmcross – 其他人也发布样本)
另外,我在一个普通的MvxViewController中使用了一个UITableView,因为我似乎无法得到MvxTableViewController来处理一个xib,这个问题似乎表明目前是不可能的。
我认为这个问题已经得到解决 – 参见MvxTableViewController.cs#L33