Search code examples
androidxamarinxamarin.androidmvvmcross

MvvmCross pass data from xml to android


I use MvvmCross for Android and I've created a ViewModel and want to pass tag attribute to ViewModel. How can I do it?

Here is example:

<LinearLayout
android:id="@+id/productsLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/products_category"
android:gravity="center"
android:orientation="vertical"
android:tag="93ada18e-1280-4a80-bc39-9b9f6d5b9724"
local:MvxBind="Click ShowCategoryCommand">

There is could be few layouts with hardcoded tags attributes and I need detect which one was tapped and send tag to another ViewModel


Solution

  • The simple answer should be:

    local:MvxBind="Click ShowCategoryCommand, CommandParameter=93ada18e-1280-4a80-bc39-9b9f6d5b9724"
    

    This problem is probably better solved using MvxRecyclerView. You can build a list of view models that are bound to the MvxRecyclerView and then use an MvxAsyncCommand<MyItemViewModel> to know what view model was selected.

    <MvxRecyclerView
    local:MvxBind="ItemsSource MyList; ItemClick MyModelClicked" 
    local:MvxItemTemplate="@layout/mymodelitemview" />
    

    Here's the nuget package for MvxRecyclerView: https://www.nuget.org/packages/MvvmCross.Droid.Support.V7.RecyclerView