MVVMCross Custom Control and Binding

2019-02-20 12:45发布

问题:

I have created a custom control (CustomCard) which is a subclass of the CardView control. I would like to use this control within my project in different places.

For example, I may place the CustomCard within an xml layout manually, or I may want the CustomCard to be an item in an MvxListView. The key is that I would like to re-use the code as much as possible and benefit from having control over the CustomCard class.

When the CustomCard is instantiated, I am inflating it's layout using the standard layout inflater, see code:

using System;
using Android.Animation;
using Android.Content;
using Android.Support.V7.Widget;
using Android.Util;
using Android.Views;
using Android.Widget;
public class Card : CardView
{

    private readonly Context _context;

    public Card(Context context)
        : base(context)
    {
        _context = context;
        Init();
    }

    public Card(Context context, IAttributeSet attrs)
        : base(context, attrs)
    {
        _context = context;
        Init();
    }

    private void Init()
    {
        var inflater = (LayoutInflater) _context.GetSystemService(Context.LayoutInflaterService);
        CardView = inflater.Inflate(Resource.Layout.base_card, this);
    }
}

Within the layout base_card.xml, I have some elements that I would like to bind using MVVMCross, for example,

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/white">
<RelativeLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:local="http://schemas.android.com/apk/res-auto"
  android:id="@+id/basecard_title"
  android:layout_width="match_parent"
  android:layout_height="match_parent">
    <!-- Title Text-->
    <TextView
    android:id="@+id/tv_basecard_header_title"
    style="@style/card.title"
    android:text="title text"
    local:MvxBind="Text Title"
    />
    <!-- ImageView -->
    <MvxImageView
    android:id="@+id/ib_basecard_header_button_expand"
    style="@style/card.image"
    local:MvxBind="Bitmap ImageBytes,Converter=InMemoryImage"/>
  </RelativeLayout>
</FrameLayout>

My actual base_card layout is much more complex.

If I try to use my CustomCard within another XML Layout, none of the binding takes place. I think this is because I am using the standard layout inflater to inflate my base_card within my CustomCard rather than BindingInflate() but I can't be sure.

I have searched on SO and through the forums but I can't find any references to anyone using a custom control that inflates it's own view when instantiated with MVVMCross binding.

Has anyone done it, or am I trying to do something that isn't possible?

回答1:

I ran into similar issue with CardView control. Since CardView directly inherits from FrameLayout I decided to use implementation almost identical to MvxFrameControl (Thanks Stuart for pointing out MvxFrameControl sample):

public class MvxCardView : CardView, IMvxBindingContextOwner
    {
        private object _cachedDataContext;
        private bool _isAttachedToWindow;
        private readonly int _templateId;
        private readonly IMvxAndroidBindingContext _bindingContext;

        public MvxCardView(Context context, IAttributeSet attrs)
            : this(MvxAttributeHelpers.ReadTemplateId(context, attrs), context, attrs)
        {
        }

        public MvxCardView(int templateId, Context context, IAttributeSet attrs)
            : base(context, attrs)
        {
            _templateId = templateId;

            if (!(context is IMvxLayoutInflater))
            {
                throw Mvx.Exception("The owning Context for a MvxCardView must implement LayoutInflater");
            }

            _bindingContext = new MvxAndroidBindingContext(context, (IMvxLayoutInflater)context);
            this.DelayBind(() =>
            {
                if (Content == null && _templateId != 0)
                {
                    Mvx.Trace("DataContext is {0}", DataContext == null ? "Null" : DataContext.ToString());
                    Content = _bindingContext.BindingInflate(_templateId, this);
                }
            });
        }


        protected MvxCardView(IntPtr javaReference, JniHandleOwnership transfer)
            : base(javaReference, transfer)
        {
        }

        protected IMvxAndroidBindingContext AndroidBindingContext
        {
            get { return _bindingContext; }
        }

        public IMvxBindingContext BindingContext
        {
            get { return _bindingContext; }
            set { throw new NotImplementedException("BindingContext is readonly in the list item"); }
        }

        protected View Content { get; set; }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                this.ClearAllBindings();
                _cachedDataContext = null;
            }

            base.Dispose(disposing);
        }

        protected override void OnAttachedToWindow()
        {
            base.OnAttachedToWindow();
            _isAttachedToWindow = true;
            if (_cachedDataContext != null
                && DataContext == null)
            {
                DataContext = _cachedDataContext;
            }
        }

        protected override void OnDetachedFromWindow()
        {
            _cachedDataContext = DataContext;
            DataContext = null;
            base.OnDetachedFromWindow();
            _isAttachedToWindow = false;
        }

        [MvxSetToNullAfterBinding]
        public object DataContext
        {
            get { return _bindingContext.DataContext; }
            set
            {
                if (_isAttachedToWindow)
                {
                    _bindingContext.DataContext = value;
                }
                else
                {
                    _cachedDataContext = value;
                    if (_bindingContext.DataContext != null)
                    {
                        _bindingContext.DataContext = null;
                    }
                }
            }
        }
    }

Usage:

<YourNamespace.MvxCardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    local:MvxTemplate="@layout/base_card"
    local:MvxBind="DataContext ." />

Note: Using custom implementation also solved my problem with binding click command to CardView control using local:MvxBind="Click MyCommand", which wasn't working until subclassing CardView.