2017-02-24 12 views
1

にMvxRecyclerView項目の行動をアップ配線方法を私持って、次のAXMLファイルがあるMvxRecyclerView:のViewModel

<?xml version="1.0" encoding="utf-8"?> 
<MvvmCross.Droid.Support.V7.RecyclerView.MvxRecyclerView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    app:MvxItemTemplate="@layout/item_detail" 
    app:MvxBind="ItemsSource Items" /> 

Correspodning ViewModelには次のように定義されています。

public class ItemsViewModel : MvxViewModel 
{ 
    private ObservableCollection<Models.Item> _items; 

    public ObservableCollection<Models.Item> Items 
    { 
     get { return _items; } 
     set 
     { 
      _items = value; 
      RaisePropertyChanged(() => Items); 
     } 
    } 

    public MvxCommand CommandToBeInvokedFromItem 
    { 
     get 
     { 
      return new MvxCommand(async() => 
      { 
       await ...; 
      }); 
     } 
    } 
    ... 
} 

マイitem_detail AXMLは以下のように定義されているがこれは:

<?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" 
    android:orientation="horizontal"> 

    <TextView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content"   
    android:textSize="24dp" 
    local:MvxBind="Text Name" /> 

    <ImageButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:src="@drawable/ic_delete_forever_black_24dp" 
    local:MvxBind="Click CommandToBeInvokedFromItem"/> 

</LinearLayout> 

Model.Itemは次のように定義されています。

public class Item 
{ 
    public string Name { get; set; } 
} 

最初にTextViewが機能するItemのnameプロパティにバインドされます。しかし、ImageButtonは、ItemのプロパティではなくMvxRecylerViewがバインドされているViewModelのCommandにバインドする必要があります。 Itemは単なるModelであり、ViewModelではありません。私はそれをどのように達成するのですか?

答えて

0

MvxRecyclerのアイテム(つまり、セル全体)のクリックでコマンドを呼び出す必要がある場合、バインディングは比較的簡単です。 MvxRecyclerViewMvxBindの値をItemsSource ItemsからItemsSource Items; ItemClick CommandToBeInvokedFromItemに変更してください。 CommandToBeInvokedFromItemは、次のようになり型パラメータとしてItemを受け入れるように変更する必要があります:

public MvxCommand<Models.Item> CommandToBeInvokedFromItem 
{ 
    get 
    { 
     return new MvxCommand<Models.Item>(async() => 
     { 
      await ...; 
     }); 
    } 
} 

コマンドはImageButtonをクリックすることで、具体的上げる必要がある場合には、最も簡単な方法は、移動することになりますCommandToBeInvokedFromItemItemを持ち、ItemをMvxViewModelに継承させるか、少なくともINotifyPropertyChangedを実装します。

+0

ルーク、ありがとうございますが、それは私が望む2番目の動作です。 MvxRecyclerViewの "セル"内にあるImageButtonのクリックにコマンドを接続したいと思います。そして、はい、私はMvxViewModelから継承するItemを持たないと、それを実行できるかどうか疑問に思っていました。 – Igor

+0

'MvxViewModel'を継承せずに正常に動作するはずです。 'CommandToBeInvokedFromItem'を' Item'に移動するだけです。 –

0

ItemsViewModelにコマンドを作成すると、コマンドはItemに渡されます。

public class Item 
{ 
    public string Name { get; set; }   
    public MvxCommand CommandToBeInvokedFromItem {get;} 

    public Item(MvxCommand clickCommand) 
    { 
     CommandToBeInvokedFromItem = clickCommand; 
    } 
}