MvxListView创建code绑定模板布局 [英] MvxListView create binding for template layout from code
问题描述
可以说我有一个简单的布局与MvxListView:
Lets say I have a simple Layout with a MvxListView:
<?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/LiivControl.Client.Droid"
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"
local:MvxBind="ItemsSource AutoListItems; ItemClick AutoListItemClicked"
local:MvxItemTemplate="@layout/vbmvxautoviewlistitem" />
</LinearLayout>
我的项目模板布局如下:
My item template layout is as follows:
<?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/LiivControl.Client.Droid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip">
<TextView
android:id="@+id/list_complex_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/list_complex_caption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
我想指定在我的ItemTemplate从后面code两者的TextView元素的绑定。我不知道如何最好地去做。我想我可以在MvxListView背后的观点code做什么OnViewModelSet。我曾尝试以下,但它显然是行不通的,因为它无法找到控制。
I would like to specify the bindings for the two textview elements in my itemtemplate from code behind. I am not sure how best to go about it. I'm guess I could do something "OnViewModelSet" in the view code behind of the MvxListView. I have tried the following but for it obviously doesn't work because it can't find the control.
protected override void OnViewModelSet()
{
IVbMvxAutoListViewModel vm = base.ViewModel as IVbMvxAutoListViewModel;
TextView title = this.FindViewById<TextView>(Resource.Id.list_complex_title);
this.CreateBinding(title).For(x => x.Text).To(vm.ListItemDescriptor.TitlePropName).Apply();
TextView subTitle = this.FindViewById<TextView>(Resource.Id.list_complex_caption);
this.CreateBinding(subTitle).For(x => x.Text).To(vm.ListItemDescriptor.SubTitlePropName).Apply();
base.OnViewModelSet();
}
我的另一个想法是,以某种方式拦截的OnCreate的ItemTemplate中的看法,但在OnCreate如果我创造我的ItemTemplate布局视图code文件不会被调用。
My other thought was to somehow intercept the oncreate for the itemtemplate view but OnCreate doesn't get called if I create a view code file for my itemtemplate layout.
推荐答案
要做到在$ C $的绑定C它可能是最好的:
To do the bindings in code it's probably best to:
- 实现自定义的
MvxListViewItem
- 实现自定义的
MvxAdapter
返回自定义列表视图项 - 实现自定义的
MvxListView
使用自定义MvxAdapter
- implement a custom
MvxListViewItem
- implement a custom
MvxAdapter
to return the custom list view item - implement a custom
MvxListView
to use the customMvxAdapter
没测试过,但code这大致是:
Not tested, but the code for this is roughly:
public class CustomListItemView
: MvxListItemView
{
public MvxListItemView(Context context,
IMvxLayoutInflater layoutInflater,
object dataContext,
int templateId)
: base(context, layoutInflater, dataContext, templateId)
{
// use delay bind to create your custom bindings here
this.DelayBind(() => {
var control = this.FindViewById<TextView>(Resource.Id.list_complex_title);
var set = this.CreateBindingSet<CustomListViewItem, YourThing>();
set.Bind(control).To(vm => vm.Title);
set.Apply();
});
}
}
2。创建一个自定义的 MvxAdapter
在此覆盖 CreateBindableView
2. Create a custom MvxAdapter
In this override CreateBindableView
public class CustomAdapter
: MvxAdapter
{
public CustomAdapter(Context context)
: base(context)
{
}
protected override IMvxListItemView CreateBindableView(object dataContext, int templateId)
{
return new CustomListItemView(_context, _bindingContext.LayoutInflater, dataContext, templateId);
}
}
原文:<一href="https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxAdapter.cs#L298">https://github.com/MvvmCross/MvvmCross/blob/v3.1/Cirrious/Cirrious.MvvmCross.Binding.Droid/Views/MvxAdapter.cs#L298
这应该是简单的:
public class CustomListView
: MvxListView
{
public CustomListView(Context context, IAttributeSet attrs)
: base(context, attrs, new CustomAdapter(context))
{
}
}
只要这是你的主UI组件,这应该是可用在AXML为:
As long as this is in your main UI assembly, this should be useable in your axml as:
<CustomListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="ItemsSource AutoListItems; ItemClick AutoListItemClicked"
local:MvxItemTemplate="@layout/vbmvxautoviewlistitem" />
如果 CustomListView
不在您的主UI组件,然后有一些技巧,以获得MvvmCross您在安装过程中把它捡起来
- 看的提供了定制的Android视图组件的的<一个href="https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-providing-custom-views-android">https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-providing-custom-views-android
If CustomListView
is not in your main UI assembly, then there are some tricks to get MvvmCross to pick it up during your Setup
- see Providing Custom Android View Assemblies in https://github.com/MvvmCross/MvvmCross/wiki/Customising-using-App-and-Setup#wiki-providing-custom-views-android
以上是做到这一点(IMO)的最好方法 - 但如果你想,那么你可以做到这一点的少code。通过在应用了自定义的适配器内部绑定,并通过设置适配器的OnCreate
在活动
The above is the best way to do this (IMO) - but if you wanted to, then you could do it in less code by just applying the bindings inside the custom adapter and by setting that adapter in OnCreate
in your Activity
这篇关于MvxListView创建code绑定模板布局的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!