Xamarin.ios 将单元格绑定到字典

Xamarin.ios 将单元格绑定到字典,xamarin.ios,mvvmcross,Xamarin.ios,Mvvmcross,我遇到了一个有点奇怪的问题,我不知道如何解决。在我的iOS项目中,我有我的TableViewCell和TableSource,我的TableSource应该用如下信息填充每个单元格: private Dictionary<string, List<Item>> savedItemList; protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPat

我遇到了一个有点奇怪的问题,我不知道如何解决。在我的iOS项目中,我有我的TableViewCell和TableSource,我的TableSource应该用如下信息填充每个单元格:

private Dictionary<string, List<Item>> savedItemList;

    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        //var foo = savedItemList.ElementAt(indexPath.Section).Value;
        //var Item = foo[indexPath.Row];

        NSString cellIdentifier;
        cellIdentifier = CellsIdentifier.Key;

        var cell = (MyTableViewCell)TableView.DequeueReusableCell(cellIdentifier, indexPath);

        return cell;
    }

public override nint NumberOfSections(UITableView tableView)
{
    return savedItemList.Count;
}


public override string TitleForHeader(UITableView tableView, nint section)
{

    return savedItemList.Keys.ElementAt((int)section);
}

剩下的是默认的标签文本。如何设置绑定以从Dict读取数据?

您是如何配置ViewModel的?您应该将TableView.Source的ItemsSource绑定到ViewModel,然后在其中设置数据:

以下是视图中的绑定代码:

更新:

如果要将ItemsSource绑定到字典,只需修改GetItemAt事件:

指出了单元类将采用的模型


我也更新了,请检查。

我没有绑定问题,因为它不工作,我在访问内部值时遇到问题。var set=this.CreateBindingSet;set.Bindsource.Tovm=>vm.MyItemList.Apply;MyTable.ReloadData;如果myItemList是一个字典,则使用上面的方法传递字典,如果我将源更改为myItemList.Values,则传递列表。但是我如何从TableViewCell中访问单个项目,它现在绑定到一个ItemsAhh列表!真不敢相信我错过了超控,谢谢!
public partial class MyTableViewCell : MvxTableViewCell
{
    public static readonly NSString Key = new NSString("MyTableViewCell");
    public static readonly UINib Nib;

    protected MyTableViewCell(IntPtr handle) : base(handle)
    {
        SelectionStyle = UITableViewCellSelectionStyle.None;

        this.DelayBind(() =>
        {


            var set = this.CreateBindingSet<MyTableViewCell, Item>();
            set.Bind(LBL_NAME)
               .To(item => item.Name)
               .Apply();
        });
        // Note: this .ctor should not contain any initialization logic.
    }
}
MvxBind:Warning:  8.03 Unable to bind: source property source not found Property:Name on KeyValuePair`2
MvxBind:Warning:  8.03 Unable to bind: source property source not found Property:Name on KeyValuePair`2
var source = new TableSource(TableView);
TableView.Source = source;
TableView.RowHeight = 120f;
TableView.ReloadData();

var set = this.CreateBindingSet<FirstView, FirstViewModel>();

set.Bind(source).For(s => s.ItemsSource).To(vm => vm.ItemsGroup);

set.Apply();
public class FirstViewModel : MvxViewModel
{

    public FirstViewModel()
    {
        // Emulate three groups here.
        ItemsGroup = new List<SessionGroup>();
        for (int i=0; i<3; i++)
        {
            var list = new List<Item>();
            for (int j=0; j<10; j++)
            {
                list.Add(new Item { Name = "Section:" + i + "Item" + j });
            }
            ItemsGroup.Add(new SessionGroup("section" + i, list));
        }
    }


    private List<SessionGroup> _ItemsGroup;
    public List<SessionGroup> ItemsGroup
    {
        get
        {
            return _ItemsGroup;
        }
        set
        {
            _ItemsGroup = value;
            RaisePropertyChanged(() => ItemsGroup);
        }
    }
}

public class Item
{
    public string Name { set; get; }
}

public class SessionGroup : List<Item>
{
    public string Key { get; set; }

    public SessionGroup(string key, List<Item> items) : base(items)
    {
        Key = key;
    }
}
public class TableSource : MvxTableViewSource
{
    private static readonly NSString CellIdentifier = new NSString("MyTableViewCell");

    public TableSource(UITableView tableView)
            : base(tableView)
    {
        tableView.SeparatorStyle = UITableViewCellSeparatorStyle.None;
        tableView.RegisterNibForCellReuse(UINib.FromName("MyTableViewCell", NSBundle.MainBundle),
                                            CellIdentifier);
    }


    protected override UITableViewCell GetOrCreateCellFor(UITableView tableView, NSIndexPath indexPath, object item)
    {
        return TableView.DequeueReusableCell(CellIdentifier, indexPath);
    }


    protected override object GetItemAt(NSIndexPath indexPath)
    {
        var _sessionGroup = ItemsSource.ElementAt(indexPath.Section) as SessionGroup;
        if (_sessionGroup == null)
            return null;

        return _sessionGroup[indexPath.Row];
    }

    public override nint NumberOfSections(UITableView tableView)
    {
        return ItemsSource.Count();
    }

    public override nint RowsInSection(UITableView tableview, nint section)
    {
        var group = ItemsSource.ElementAt((int)section) as SessionGroup;
        return group.Count();
    }

    public override string TitleForHeader(UITableView tableView, nint section)
    {
        var group = ItemsSource.ElementAt((int)section) as SessionGroup;
        return string.Format($"Header for section {group.Key}");
    }
}
protected override object GetItemAt(NSIndexPath indexPath)
{
    return savedItemList.Values.ToList()[indexPath.Section][indexPath.Row];
}

public override nint NumberOfSections(UITableView tableView)
{
    if (ItemsSource != null)
    {
        savedItemList = (Dictionary<string, List<Item>>)ItemsSource;
        return savedItemList.Count();
    }           
    return 0;
}

public override nint RowsInSection(UITableView tableview, nint section)
{
    if (ItemsSource != null)
    {
        savedItemList = (Dictionary<string, List<Item>>)ItemsSource;
        return savedItemList.Values.ToList()[(int)section].Count;
    }
    return 0;
}