MvxListView 从代码创建模板布局绑定 [英] MvxListView create binding for template layout from code

查看:14
本文介绍了MvxListView 从代码创建模板布局绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有 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 中的两个 textview 元素指定绑定.我不知道如何最好地去做.我想我可以在 MvxListView 后面的视图代码中做一些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();
    }

我的另一个想法是以某种方式拦截 itemtemplate 视图的 oncreate,但如果我为 itemtemplate 布局创建视图代码文件,则不会调用 OnCreate.

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.

推荐答案

要在代码中进行绑定,最好:

To do the bindings in code it's probably best to:

  1. 实现自定义MvxListViewItem
  2. 实现自定义MvxAdapter 返回自定义列表视图项
  3. 实现自定义 MvxListView 以使用自定义 MvxAdapter
  1. implement a custom MvxListViewItem
  2. implement a custom MvxAdapter to return the custom list view item
  3. implement a custom MvxListView to use the custom MvxAdapter

未测试,但代码大致如下:

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)
    {
        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

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);
    }
}

原文:https://github.com/MvvmCross/MvvmCross/blob/v3.1/Crillous/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 在你的 Setup 中获取它 - 请参阅提供自定义 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) - 但如果您愿意,那么您可以通过在自定义适配器中应用绑定并在 OnCreate<中设置该适配器以更少的代码来完成/code> 在您的 Activity

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 从代码创建模板布局绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆