您对点击事件尝试绑定的位置的分析绝对是正确的。
我通常采取两种方法:
- 使用 ItemClick 列表
- 继续使用 Click 但在 ViewModel 端进行一些重定向。
So...1
The 主菜单 https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20Tutorial/Tutorial/Tutorial.Core/ViewModels/MainMenuViewModel.cs在教程中有一个 ViewModel 有点像:
public class MainMenuViewModel
: MvxViewModel
{
public List<T> Items { get; set; }
public IMvxCommand ShowItemCommand
{
get
{
return new MvxRelayCommand<T>((item) => /* do action with item */ );
}
}
}
这在 axml 中使用如下:
<Mvx.MvxBindableListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res/Tutorial.UI.Droid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
local:MvxBind="{'ItemsSource':{'Path':'Items'},'ItemClick':{'Path':'ShowItemCommand'}}"
local:MvxItemTemplate="@layout/listitem_viewmodel"
/>
此方法只能对整个列表项上的 ItemClick 执行 - 而不能对列表项中的各个子视图执行。
Or...2
因为我们没有任何RelativeSource
mvx 中的绑定指令,这种类型的重定向可以在 ViewModel/Model 代码中完成。
这可以通过提供模型对象的启用行为的包装器而不是模型对象本身来完成 - 例如用一个List<ActiveArticle>
:
public ActiveArticle
{
Article _article;
ArticleViewModel _parent;
public WrappedArticle(Article article, ArticleViewModel parent)
{
/* assignment */
}
public IMvxCommand TheCommand { get { return MvxRelayCommand(() -> _parent.DoStuff(_article)); } }
public Article TheArticle { get { return _article; } }
}
然后你的 axml 必须使用如下绑定:
<TextView ...
local:MvxBind="{'Text':{'Path':'TheArticle.Label'}}" />
and
<ImageButton
...
local:MvxBind="{'Click':{'Path':'TheCommand.MyTest'}}" />
这种方法的一个例子是会议样本,它使用使用命令 https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/Helpers/WithCommand.cs
但是...请注意,使用时WithCommand<T>
我们发现了内存泄漏 - 基本上 GarbageCollection 拒绝收集嵌入的MvxRelayCommand
- 这就是为什么WithCommand<T>
is IDisposable
以及为什么基本会话列表视图模型 https://github.com/slodge/MvvmCross/blob/master/Sample%20-%20CirriousConference/Cirrious.Conference.Core/ViewModels/SessionLists/BaseSessionListViewModel.cs当视图分离时,清除列表并释放 WithCommand 元素。
评论后更新:
如果您的数据列表很大 - 并且您的数据是固定的(您的文章是没有 PropertyChanged 的模型)并且您不想承担创建大型数据的开销List<WrappedArticle>
那么解决这个问题的一种方法可能是使用WrappingList<T>
class.
这与 Microsoft 代码中采用的方法非常相似 - 例如在 WP7/Silverlight 中的虚拟化列表中 -http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx http://shawnoster.com/blog/post/Improving-ListBox-Performance-in-Silverlight-for-Windows-Phone-7-Data-Virtualization.aspx
对于您的文章,这可能是:
public class ArticleViewModel: MvxViewModel
{
public WrappingList<Article> Articles;
// normal members...
}
public class Article
{
public string Label { get; set; }
public string Remark { get; set; }
}
public class WrappingList<T> : IList<WrappingList<T>.Wrapped>
{
public class Wrapped
{
public IMvxCommand Command1 { get; set; }
public IMvxCommand Command2 { get; set; }
public IMvxCommand Command3 { get; set; }
public IMvxCommand Command4 { get; set; }
public T TheItem { get; set; }
}
private readonly List<T> _realList;
private readonly Action<T>[] _realAction1;
private readonly Action<T>[] _realAction2;
private readonly Action<T>[] _realAction3;
private readonly Action<T>[] _realAction4;
public WrappingList(List<T> realList, Action<T> realAction)
{
_realList = realList;
_realAction = realAction;
}
private Wrapped Wrap(T item)
{
return new Wrapped()
{
Command1 = new MvxRelayCommand(() => _realAction1(item)),
Command2 = new MvxRelayCommand(() => _realAction2(item)),
Command3 = new MvxRelayCommand(() => _realAction3(item)),
Command4 = new MvxRelayCommand(() => _realAction4(item)),
TheItem = item
};
}
#region Implementation of Key required methods
public int Count { get { return _realList.Count; } }
public Wrapped this[int index]
{
get { return Wrap(_realList[index]); }
set { throw new NotImplementedException(); }
}
#endregion
#region NonImplementation of other methods
public IEnumerator<Wrapped> GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public void Add(Wrapped item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(Wrapped item)
{
throw new NotImplementedException();
}
public void CopyTo(Wrapped[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(Wrapped item)
{
throw new NotImplementedException();
}
public bool IsReadOnly { get; private set; }
#endregion
#region Implementation of IList<DateFilter>
public int IndexOf(Wrapped item)
{
throw new NotImplementedException();
}
public void Insert(int index, Wrapped item)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
#endregion
}