2014-11-05 43 views
0

我有一个绑定Click事件对我的项目MvxGridView(我正在使用ItemClick绑定)的问题。ItemClick未绑定在MvxGridView的按钮上

所有渲染都很好,而且东西。当我使用TextView时,我的GoClick方法正常启动。但是当我在我的ItemTemplateView中将TextView更改为Button时,则不再调用GoClick方法。

根据this答案(选项编号1)都应该正常工作。但在Button的情况下,它不。

任何帮助表示赞赏,我卡在这里。

我的视图模型:

public class MyViewModel : MvxViewModel 
{ 
    private IEnumerable<MyListItem> items; 

    public MyViewModel() 
    { 
     this.items = new List<MyListItem> 
     { 
      new MyListItem {Name = "Item1"}, 
      new MyListItem {Name = "Item2"}, 
     }; 
    } 

    public IEnumerable<MyListItem> Items 
    { 
     get { return this.items; } 
    } 

    public ICommand SelectItem 
    { 
     get { return new MvxCommand<MyListItem>(this.GoClick);} 
    } 

    public void GoClick(MyListItem item) 
    { 
     //doSomething 
    } 
} 

我与MvxGridView布局:

<?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:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:background="@color/black" 
    > 
     <Mvx.MvxGridView 
      android:layout_width="300dp" 
      android:layout_height="match_parent" 
      android:gravity="center" 
      android:horizontalSpacing="10dp" 
      android:numColumns="3" 
      android:stretchMode="columnWidth" 
      android:verticalSpacing="10dp" 
      local:MvxItemTemplate="@layout/itemtemplateview" 
      local:MvxBind="ItemsSource Items; ItemClick SelectItem" 
      /> 
</LinearLayout> 

ItemTemplateView如果你想要一个click事件(与TextView正常工作)

<?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:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    > 
     <TextView 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      local:MvxBind="Text Name" 
      /> 
     <!-- CLICK ON THIS BUTTON DOES NOT WORK --> 
     <!-- 
     <Button 
      android:layout_height="wrap_content" 
      android:layout_width="match_parent" 
      local:MvxBind="Text Name" 
      /> 
     --> 
</LinearLayout> 
+0

这是接近的问题,如http://stackoverflow.com/questions/14443364/unable-to-get-listview-itemclick-to-be-called-in-monodroid – Stuart 2014-11-05 17:14:51

+0

它是相似的,但我想知道这是真的需要为此创建自定义适配器?我试图在itemView.xml中设置这些属性,但没有运气 – 2014-11-06 08:55:02

+0

我认为你可能只是面临一个Android问题 - 搜索周围的问与答像http://stackoverflow.com/questions/6703390/listview-setonitemclicklistener-not-working-通过添加按钮 - 如果你尝试了一些东西,那么请编辑问题以提供更多信息(评论很难理解) – Stuart 2014-11-06 12:09:45

回答

0

按钮,并且您正在使用“瑞士”绑定,请使用:

local:MvxBind="Click DoNameClick"/> 

代替

local:MvxBind="Text Name" 

退房文档:https://github.com/MvvmCross/MvvmCross/wiki/databinding

注 - 你必须有一个公共的 “DoNameClick” 吸气剂您的视图模型!