ボタンを含むテンプレートを含むリストビューがあります。ボタンをクリックすると、イベントが発生してリストビュー行の値が返されるので、それを使用してデータベースに追加できます。私の問題は、ボタン イベントをアイテム テンプレートにバインドする方法がわからないことです。私はいくつかのアプローチを試みましたが、これまでのところ成功していません。
マイリストビュー:
<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>
私のアイテムテンプレート:
<?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>
私のビューモデル:
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
}
}
テンプレートのボタンとリストビューに local:MvxBind="Click OrderBtnClick" を配置しました。itemtemplate からボタンを削除すると ItemClick が機能するように見えますが、それは私が探しているものではありません。ボタンでイベントをトリガーしたい。誰かが私を正しい方向に向けることができますか?
更新:
ここに投稿された2番目の提案スチュアートロッジを試しました。ここに私のラッパークラスがあります:
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; } }
}
私のビューモデル:
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>
私のリストビューは完全に機能します。すべてのプロパティが正しくバインドされ、名前、簡単な説明、画像が表示されます。機能しないのはボタンクリックです。アプリケーションの出力で、次のようなエラーが表示されます: MvxBind:Warning: 76.06 Unable to bind: source property source not found Cirrious.MvvmCross.Binding.Parse.PropertyPath.PropertyTokens.MvxPropertyNamePropertyToken on MenuItemWrap
私はそれを修正するためにいくつかのアプローチを試みましたが、成功しませんでした。MvvMCross アセンブリで RelayCommand クラスが見つからなかったため、ここからコードをプロジェクトにコピーして貼り付けました。