axml MVVMCROSS 中嵌入式布局的绑定问题
Binding issues for embedded layout in axml MVVMCROSS
我有一个布局,它有点像页脚视图,几乎包含在所有布局中。
假设我们以一个 textview 为例,它不直接出现在 activity 布局中,而是在布局中包含的不同布局中。
示例案例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="0dp"
android:layout_gravity="bottom"
android:gravity="bottom|center">
<ImageView
android:id="@+id/img_awesome_intro"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_awesome_intro"
android:layout_marginBottom="@dimen/dp_awesome_intro_margin_bottom"
android:scaleType="fitXY"
android:src="@drawable/ic_welcome_intro" />
<include
layout="@layout/BarButton"
android:id="@+id/btn_welcome_join"
android:layout_width="fill_parent"
android:layout_height="@dimen/dp_awesome_learnmore_size" />
<include
layout="@layout/BarButton"
android:layout_width="fill_parent"
android:id="@+id/btn_welcome_learnmore"
android:layout_height="@dimen/dp_awesome_learnmore_size" />
</LinearLayout>
包含的BarButton布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#229DCF"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="@dimen/dp_button_margin_right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="@dimen/sp_button_title"
android:textColor="@color/white"
android:layout_gravity="center_horizontal|center_vertical"
android:id="@+id/titleLbl" />
<ImageView
android:id="@+id/iconImg"
android:layout_width="@dimen/dp_button_imgsize"
android:layout_height="@dimen/dp_button_imgsize"
android:layout_marginTop="5px"
android:layout_marginLeft="@dimen/dp_button_margin_betweentxt"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@drawable/ic_forward_arrow" />
</LinearLayout>
我的问题是如何将 Textview 的点击事件绑定到 viewmodel 的命令
此文本视图存在于 BarButton 布局中,而不是直接在 activity 布局中。
您仍然可以绑定到 TextView
。 include 只是 Android's layout system 提供的一种模式,用于创建可重复使用的布局。创建布局树时,它会将 include 中定义的元素添加到现有布局中。
To efficiently re-use complete layouts, you can use the <include/>
and
<merge/>
tags to embed another layout inside the current layout.
然后您可以为您的命令使用 XML 或 Fluent 绑定。
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="@dimen/sp_button_title"
android:textColor="@color/white"
android:layout_gravity="center_horizontal|center_vertical"
android:id="@+id/titleLbl"
app:MvxBind="Click MyCommand"/>
var title = FindViewById<TextView>(Resource.Id.titleLbl);
...
bindingSet.Bind(title)
.For(c => c.BindClick())
.To(vm => vm.MyCommand)
.Apply();
如果您想减少此命令在 ViewModel
(Pages/layouts) 之间的重复,您可以在基础 class.[=21 中共享 ViewModel
命令=]
我有一个布局,它有点像页脚视图,几乎包含在所有布局中。
假设我们以一个 textview 为例,它不直接出现在 activity 布局中,而是在布局中包含的不同布局中。
示例案例:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="0dp"
android:layout_gravity="bottom"
android:gravity="bottom|center">
<ImageView
android:id="@+id/img_awesome_intro"
android:layout_width="match_parent"
android:layout_height="@dimen/dp_awesome_intro"
android:layout_marginBottom="@dimen/dp_awesome_intro_margin_bottom"
android:scaleType="fitXY"
android:src="@drawable/ic_welcome_intro" />
<include
layout="@layout/BarButton"
android:id="@+id/btn_welcome_join"
android:layout_width="fill_parent"
android:layout_height="@dimen/dp_awesome_learnmore_size" />
<include
layout="@layout/BarButton"
android:layout_width="fill_parent"
android:id="@+id/btn_welcome_learnmore"
android:layout_height="@dimen/dp_awesome_learnmore_size" />
</LinearLayout>
包含的BarButton布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#229DCF"
android:orientation="horizontal"
android:paddingLeft="15dp"
android:paddingRight="@dimen/dp_button_margin_right"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="@dimen/sp_button_title"
android:textColor="@color/white"
android:layout_gravity="center_horizontal|center_vertical"
android:id="@+id/titleLbl" />
<ImageView
android:id="@+id/iconImg"
android:layout_width="@dimen/dp_button_imgsize"
android:layout_height="@dimen/dp_button_imgsize"
android:layout_marginTop="5px"
android:layout_marginLeft="@dimen/dp_button_margin_betweentxt"
android:layout_gravity="center_horizontal|center_vertical"
android:src="@drawable/ic_forward_arrow" />
</LinearLayout>
我的问题是如何将 Textview 的点击事件绑定到 viewmodel 的命令 此文本视图存在于 BarButton 布局中,而不是直接在 activity 布局中。
您仍然可以绑定到 TextView
。 include 只是 Android's layout system 提供的一种模式,用于创建可重复使用的布局。创建布局树时,它会将 include 中定义的元素添加到现有布局中。
To efficiently re-use complete layouts, you can use the
<include/>
and<merge/>
tags to embed another layout inside the current layout.
然后您可以为您的命令使用 XML 或 Fluent 绑定。
<TextView
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:textSize="@dimen/sp_button_title"
android:textColor="@color/white"
android:layout_gravity="center_horizontal|center_vertical"
android:id="@+id/titleLbl"
app:MvxBind="Click MyCommand"/>
var title = FindViewById<TextView>(Resource.Id.titleLbl);
...
bindingSet.Bind(title)
.For(c => c.BindClick())
.To(vm => vm.MyCommand)
.Apply();
如果您想减少此命令在 ViewModel
(Pages/layouts) 之间的重复,您可以在基础 class.[=21 中共享 ViewModel
命令=]