为了在 MvvmCross 中找到缺席 RelativeSource,我使用了 Stuart 的建议并在 MvxBindableListView 中实现 了 WrappingList MVVMCross 更改 ViewModel
但是,我在每次发生绑定时都会看到此跟踪,我想知道它有多糟糕:
绑定到 IEnumerable 而不是 IList - 这可能效率低下,尤其是对于大型列表
也许还有其他建议?
public class WrappingCommandsList<T> : IList<WrappingCommandsList<T>.Wrapped>
{
private readonly List<T> _realList;
private readonly Action<T> _realActionOnClick;
public class Wrapped
{
public IMvxCommand ClickCommand { get; set; }
public T TheItem { get; set; }
}
public WrappingCommandsList(List<T> realList, Action<T> realActionOnClick)
{
_realList = realList;
_realActionOnClick = realActionOnClick;
}
private Wrapped Wrap(T item)
{
return new Wrapped()
{
ClickCommand = new MvxCommand(() => _realActionOnClick(item)),
TheItem = item
};
}
public WrappingCommandsList<T>.Wrapped this[int index]
{
get
{
return Wrap(_realList[index]);
}
set
{
throw new NotImplementedException();
}
}
public int Count
{
get { return _realList.Count; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public IEnumerator<WrappingCommandsList<T>.Wrapped> GetEnumerator()
{
foreach (var item in _realList)
{
yield return Wrap(item);
}
}