Question

Is it possible to bind an object X to the include tag, so that the context of binding in the included layout is X? I want to use a layout multiple times, but not in a list.

MainLayout.xml

...
<include
    android:id="@+id/btnDealerMap"
    android:layout_width="64dp"
    android:layout_height="64dp"
    android:visibility="gone"
    layout="@layout/ServiceBarButtonPhone" 
    local:MvxBind="??? X" />
...

ServiceBarButtonPhone.xml

Title and Text are properties of X.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:local="http://schemas.android.com/apk/res/..."
    android:layout_width="96dp"
    android:layout_height="96dp">
    <TextView
        ...
        android:id="@+id/txtTitle"
        local:MvxBind="Text Title" />
    <TextView
        ...
        android:id="@+id/txtText"
        local:MvxBind="Text Text" />
</RelativeLayout>
Was it helpful?

Solution

There's nothing to bind on the outside of the include - but you can put normal binding on the inside of the file - it then just gets included in at compile time. See one example in: https://github.com/MvvmCross/MvvmCross-Tutorials/blob/master/ApiExamples/ApiExamples.Droid/Resources/Layout/Test_If.axml

If instead you want an inner 'layout' with its own DataContext, try MvxView - for an example of that see MvxFrameControl - as used in part of N=26 - see https://github.com/MvvmCross/NPlus1DaysOfMvvmCross/blob/b405eef7dddf4d65b00116e142855653eae9f06b/N-26-Fraggle/Rock.Droid/Resources/Layout/FirstView.axml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top