我有一个包含按钮的模板的列表视图。单击按钮时,我想要触发事件并返回listview行的值,因此我可以使用它将其添加到数据库中。我的问题是,我不知道如何将我的buttonevent绑定到itemtemplate。我尝试了一些方法但到目前为止没有成功。
我的列表视图:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:divider="#aeaeae"
android:dividerHeight="1px"
local:MvxBind="ItemsSource MenuCollection; ItemClick OrderBtnClick"
local:MvxItemTemplate="@layout/listitem_menuitem" />
</LinearLayout>
我的ItemTemplate:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
我的ViewModel:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = _sqlSvc.MenuItemGetAll ();
}
private List<MenuItem> _menuCollection = new List<MenuItem> ();
public List<MenuItem> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand(btnClick);
return _orderBtnClick;}
}
private void btnClick()
{
//Do Something
}
}
我在模板和列表视图上的按钮上放置了本地:MvxBind =“Click OrderBtnClick”。当我从itemtemplate中删除按钮时,ItemClick似乎工作,但这不是我正在寻找的。我希望按钮能够触发事件。有人能指出我正确的方向吗?
更新
我已经尝试过stuart lodge发布的第二个建议here。这是我的包装类:
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap ()
{
}
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand Click {
get {
return new MvxRelayCommand (() => _parent.btnClick(WrapConverter.ConvertToWrapMenuItem(_mnuItem, _parent)));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
我的ViewModel:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass(_sqlSvc.MenuItemGetAll (), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick{
get{
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItemWrap> (btnClick);
return _orderBtnClick;
}
}
public void btnClick(MenuItemWrap item)
{
MenuCollection.Clear ();
}
}
这是我的模板:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Mvx.MvxImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="10dp"
local:MvxBind="ImageUrl Item.ImageUrl" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="30dp"
android:textSize="40dp"
local:MvxBind="Text Item.Name" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:textSize="20dp"
local:MvxBind="Text Item.ShortDescription" />
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click btnClick.OrderBtnClick"
android:id="@+id/button1" />
</LinearLayout>
</LinearLayout>
我的listview完美无缺。所有属性都正确绑定,我可以看到名称,短描述和图像。什么不起作用是按钮单击。在我的应用程序输出中,我收到一条错误消息: MvxBind:警告:76.06无法绑定:未找到源属性源Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on MenuItemWrap
我尝试了一些方法来修复它,但没有成功。我会提到我没有在MvvMCross程序集中找到RelayCommand类,所以我将代码从here粘贴到我的项目中。
答案 0 :(得分:4)
我找到了解决方案。问题是点击绑定。您应该只引用包装类中的操作,而不是两者。这是我的包装类&amp; listview itemtemplate。
ItemTemplate:
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:minWidth="25px"
android:minHeight="25px">
<Button
android:layout_width="wrap_content"
android:layout_height="70dip"
android:layout_alignParentRight="true"
android:layout_marginTop="20dip"
android:layout_marginRight="20dip"
android:layout_gravity="right|center_vertical"
android:text="Bestel"
local:MvxBind="Click OrderClick" />
</LinearLayout>
<强> WrapperClass:强>
public class MenuItemWrap
{
MenuItem _mnuItem;
ListPresentationViewModel _parent;
public MenuItemWrap (MenuItem item, ListPresentationViewModel parent)
{
_mnuItem = item;
_parent = parent;
}
public IMvxCommand OrderClick {
get {
return new MvxCommand (() => _parent.btnClick (_mnuItem));
}
}
public MenuItem Item{ get { return _mnuItem; } }
}
我的ViewModel:
public class ListPresentationViewModel: MvxViewModel
{
private readonly ISQLService _sqlSvc;
public ListPresentationViewModel (ISQLService sqlService)
{
_sqlSvc = sqlService;
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemGetAll(), this);
}
private int _catId;
public int CategorieId {
get{ return _catId;}
set{
_catId = value;
ChangeMenuCollection ();
}
}
private void ChangeMenuCollection()
{
MenuCollection = WrapConverter.ConvertToWrapperClass (_sqlSvc.MenuItemByCategorie (_catId), this);
}
private List<MenuItemWrap> _menuCollection = new List<MenuItemWrap> ();
public List<MenuItemWrap> MenuCollection {
get{ return _menuCollection;}
set {
_menuCollection = value;
RaisePropertyChanged (() => MenuCollection);
}
}
private IMvxCommand _orderBtnClick;
public IMvxCommand OrderBtnClick {
get {
_orderBtnClick = _orderBtnClick ?? new MvxCommand<MenuItem> (btnClick);
return _orderBtnClick;
}
}
public void btnClick (MenuItem item)
{
//Do Something
}
}
答案 1 :(得分:1)
MvxRelayCommand
在第3版中缩短为MvxCommand
- 只是为了节省打字。Click btnClick.OrderBtnClick
- 但您的MenuItemWrap
类没有btnClick
属性 - 所以您实际上想绑定{{1 }}?