【发布时间】:2015-03-11 17:16:02
【问题描述】:
我创建了一个自定义控件 (CustomCard),它是 CardView 控件的子类。我想在我的项目中的不同地方使用这个控件。
例如,我可以手动将 CustomCard 放置在 xml 布局中,或者我可能希望 CustomCard 成为 MvxListView 中的一个项目。关键是我想尽可能地重用代码并从对 CustomCard 类的控制中受益。
当 CustomCard 被实例化时,我正在使用标准布局充气器充气它的布局,请参见代码:
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);
}
}
在布局 base_card.xml 中,我有一些我想使用 MVVMCross 绑定的元素,例如,
<?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>
我的实际 base_card 布局要复杂得多。
如果我尝试在另一个 XML 布局中使用我的 CustomCard,则不会发生任何绑定。我认为这是因为我使用标准布局充气器在 CustomCard 中充气我的 base_card 而不是 BindingInflate() 但我不能确定。
我在 SO 和论坛上进行了搜索,但我找不到任何对使用自定义控件的任何人的引用,该自定义控件在使用 MVVMCross 绑定实例化时会膨胀它自己的视图。
有人做过吗,还是我在尝试做一些不可能的事情?
【问题讨论】:
-
您可能会扩展此绑定库中所做的工作:github.com/kjeremy/MvxAndroid.Support.V7.AppCompat/tree/master/…
-
太好了,谢谢指点。
标签: xamarin xamarin.android mvvmcross